This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Synchronized block doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Synchronized block doubt" Watch "Synchronized block doubt" New topic
Author

Synchronized block doubt

archit thakur
Greenhorn

Joined: Jul 10, 2010
Posts: 24
Hi,

As per my understanding, the difference between Synchronized method and Synchronized block is that in synchronized method...compiler doesn't allow any other thread to go in the method for a particular object...if some other thread is inside the same method for the same object but that object could be any object and in synchronized block..we specify that object for which the method call has to be synchronized

but if I try to run the following code,



output
{{{onethreetwo}
}
}

which was expected also

but for the following code:



output:
{one}
{two}
{three}

why the method is synchronized when the argument specified in the synchronized block is different from what is used in calling the method.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

archit thakur wrote:Hi,
As per my understanding, the difference between Synchronized method and Synchronized block is


A synchronized method is just syntactic sugar for a synchronized block. Declaring an instance method (non-static method) synchronized is the same as just putting synchronized (this) { } around the whole method body. Declaring a class method (static method) synchronized is the same as just putting synchronized (ThisClass.class) { } around the whole method body (where ThisClass is replaced by the actual class name, of course).

Syncing always does exactly the same thing: Obtains the object's lock, first waiting until it's released if some other thread currently holds it. As far as mutual exclusion goes, that's all it does.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

archit thakur wrote:why the method is synchronized when the argument specified in the synchronized block is different from what is used in calling the method.

Well, technically, the method is not synchronized. It is being called from a synchronized block.

Now, in your synchronized block, you are synchronizing on 'callme', and this reference is referring to same object for all 3 threads. Since the synchronization is done on same object for all 3 threads, only one thread can enter in that block.

I hope this helps.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Synchronized block doubt