CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Interesting fact about synchronized method. (http://www.codingforums.com/showthread.php?t=281239)

Zaid 11-06-2012 02:40 PM

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?

Fou-Lu 11-06-2012 03:52 PM

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.

Zaid 11-06-2012 05:24 PM

Quote:

Originally Posted by Fou-Lu (Post 1289336)
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

Fou-Lu 11-06-2012 07:13 PM

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.


All times are GMT +1. The time now is 01:23 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.