Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Interesting fact about synchronized method.

    Code:
    class A
    {
    	int x=1,y=1;
    	static int z;
    	synchronized void f1()
    	{
    		x++;
    		z++;
    		System.out.println("Called "+z+" Time");
    		try
    		{
    			Thread.sleep(10000);
    		}
    		catch(InterruptedException e){}
    		System.out.println("(Synchronized)X= "+x);
    	}	
    }
    
    class B extends Thread
    {
    	A ab;
    	B(A aob)
    	{
    		ab=aob;
    	}
    	
    	public void run()
    	{
    		ab.f1();
    	}
    }
    
    class Multi_sync
    {
    	public static void main(String args[])
    	{
    		A a1=new A();
    		A a2=new A();
    		B b1=new B(a1);
    		B b2=new B(a2);
    		b1.start();
    		b2.start();
    	}
    }
    The output of the program is

    Called 1 Time
    Called 2 Time
    (Synchronized)X= 2
    (Synchronized)X= 2

    Although it should be,
    Called 1 Time
    (Synchronized)X= 2
    Called 2 Time
    (Synchronized)X= 2

    I don't understand the o/p, because I think if a synchronized method is invoked for the first time, it won't get executed for the 2nd until the first invocation returns the control....is it right?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    a1 and a2 are running through b1 and b2. These are threads of their own, so you are not doing anything to block the other objects, so issuing a sleep on that running thread won't impact another thread. There are many ways to do this from the simple implicit locks of the object, to running with ExecutorService and pools or writing your own threads and runnables. But you'll need to write the blocking conditions for the thread.

  • #3
    New Coder
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by Fou-Lu View Post
    a1 and a2 are running through b1 and b2. These are threads of their own, so you are not doing anything to block the other objects, so issuing a sleep on that running thread won't impact another thread. There are many ways to do this from the simple implicit locks of the object, to running with ExecutorService and pools or writing your own threads and runnables. But you'll need to write the blocking conditions for the thread.
    I think u got it wrong...My question was that if a synchronized method is called by a thread then it should not allow its control to other threads until it releases control for the 1st thread....
    I guess when a synchronized method is called by the same reference for the 2nd time then it serializes it(blocks it until the first call returns control)...In the above case, I have used different references(a1 and a2) and although they call the same method they are allow concurrent access without synchronization. Correct if I am wrong

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Uh, no. The synchronized functionality in Java's purpose is to maintain integrity of an instance of an object, not multiple instances of an object. Multiple threads is of course the purpose, but when each of these threads has an instance of their own to work with, then there is no impact of using the synchronization functionality from one object into the next object. Synchronized can also be done on static methods as well as blocks (my preference if I run synchronized).
    So in short, its purpose is to handle mutual access to a shared object. Give both instances of new B call the a1, and it will run as you expect. To implement shared handling on the class itself, you need to handle the threading at a higher level including using locks.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •