jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difficulty in understanding synchronization? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difficulty in understanding synchronization?" Watch "difficulty in understanding synchronization?" New topic

difficulty in understanding synchronization?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924

i was reading synchronization from http://javarevisited.blogspot.in/2011/04/synchronization-in-java-synchronized.html. i came across a point which i was not able to understand. i am stating it below:

Java synchronized block is better than java synchronized method in java because by using synchronized block you can only lock critical section of code and avoid locking whole method which can possibly degrade performance. A good example of java synchronization around this concept is getInstance() method Singleton class.

my question is that the link says, synchronization block locks critical section of code(highlighted in bold in above paragraph). my confusion is that don't we lock objects as opposed to section of code. also the kb6 book says that once we enter a synchronized method we acquire lock on the object whose method code we are running, and further , no other thread is allowed to enter any other SYNCHRONIZED METHOD of that object(non synchronized methods can be accessed by multiple threads). so my question is that when we use synchronization blocks and we acquire a lock on an object , say which does not have any synchronized method, then what is the thing on which we get mutual exclusive access ?

Ankit Garg

Joined: Aug 03, 2008
Posts: 9466

You are right that synchronization acquires lock on an object. Lets see it this way. Here's a method in which there are some thread safe statements (which don't need synchronization) and some thread unsafe statements (which needs synchronization). (Note: Don't try to make out any meaning of this code its just my imagination at run )

Now synchronizing the method is like synchronizing all the statements in the method in a synchronized block

But the first 2 operations don't need synchronization and can be performed by 2 threads simultaneously. So its better that I don't synchronize those two statements

This flexibility is only there in synchronized blocks...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

A synchronized block can help reduce the number of lines of code on which synchronization should be done. Ankit's example helps illustrate that.

A Singleton's getInstance() can be improved using double checked locking (DCL), which is also an example of using synchronized blocks instead of synchronizing the entire method. This wiki entry sheds more light on this topic - http://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java

DCLs are not the only way to expose a Singleton instance. Take a look at the book 'Effective java' to explore this topic a little more. (Not related to the cert in any way, but it is a great source of good design principles)

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
I agree. Here's the link: http://aspose.com/file-tools
subject: difficulty in understanding synchronization?
It's not a secret anymore!