• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

difficulty in understanding synchronization?

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
reply
    Bookmark Topic Watch Topic
  • New Topic