wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Synchronised block explanation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronised block explanation" Watch "Synchronised block explanation" New topic
Author

Synchronised block explanation

Vibhor Chandel
Greenhorn

Joined: Apr 05, 2012
Posts: 5
Hi,

Can anyone be kind enough to explain this synchronization:



The above synchronized block is taking "msg" as a monitor and not the object that the display method will be called on. Can you explain what might be some cases where this can be useful and is it the same as doing synchronized(this).

Prompt help is greatly appreciated.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
It really isn't useful for anything, in my opinion. It probably indicates that the person who wrote it was confused, and should not be trusted to write code in a concurrent environment.
Vibhor Chandel
Greenhorn

Joined: Apr 05, 2012
Posts: 5
Hi Mike,

That's what I need to know, if its not good then why is it not good. Is the synchronized block no synchronized this way. Just want some knowledge about the monitor objects and the role they play in making a block synchronized.

Thanks
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Vibhor Chandel wrote:Hi Mike,

That's what I need to know, if its not good then why is it not good. Is the synchronized block no synchronized this way. Just want some knowledge about the monitor objects and the role they play in making a block synchronized.

Thanks


As far as the Java language and the mutual exclusion behavior of the code is concerned, it doesn't matter what object you sync on. All that matters in that respect is that all threads accessing a particular shared resource sync on the same lock.

Now, having said that, some objects may be more or less appropriate as locks in some situations. The two most common are:

1) Sync on the mutable object that's being modified. For instance, this is what Collections.syncrhonizedXxx() does. All the methods on the returned collection are synced on that collection, and when you iterate over that collection, you must also sync on that collection around your iteration loop. This way, all uses of that mutable object are synced on the same lock--that object's lock.

2) Sync on an Object created just for the purpose of being a mutex lock. This is useful when you want to ensure exclusive access within your class, but you don't want to use an object that anybody else can also possibly grab as a lock. You make a private final Object lock = new Object(); field and sync on that.

There really should be no reason to ever sync on a String. And doing so can have negative repercussions. For instance, if the String happens to be one that's in the constant pool, then it's too easy for many unrelated groups of threads to be syncing on the same String.
Vibhor Chandel
Greenhorn

Joined: Apr 05, 2012
Posts: 5
So in the code i have above I get a lock on the String msg and get the code inside the block synchronized. Isn't this the same thing if we have synchronized the display method instead. I mean both will restrict access to the block right?
Vibhor Chandel
Greenhorn

Joined: Apr 05, 2012
Posts: 5
sorry I missed a few braces in the above code here is the right one.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Vibhor Chandel wrote:So in the code i have above I get a lock on the String msg and get the code inside the block synchronized. Isn't this the same thing if we have synchronized the display method instead. I mean both will restrict access to the block right?


Only if other calls to display are syncing on the same String. If T1 calls display("x"); and T2 calls display("y"); then both can execute at the same time. Remember, syncing only prevents access to a block or method when all the threads in question sync on the same lock.

If you declare the display method as synced, then its as if its body was synced on this, so every thread calling display on that particular object will have to go in one at a time.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Vibhor Chandel wrote:sorry I missed a few braces in the above code here is the right one.


I don't see any difference other than indentation, but, regardless, everything we've said still applies.
Vibhor Chandel
Greenhorn

Joined: Apr 05, 2012
Posts: 5
its because I edited the top one later. Thanks for the reply Jeff.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Vibhor Chandel wrote:its because I edited the top one later. Thanks for the reply Jeff.


You're welcome! Happy to help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronised block explanation
 
Similar Threads
Question on synchronize block
Difference between synchronized method and synchrnoized block of code
Synchronized Block and Synchronized method
Program on Threads
JComboBox switch statement MouseEvent problem