This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need clarification on the way synchronization works?

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
following is paragraph from kb 6 book page no. 735.

With locks. Every object in Java has a built-in lock
that only comes into play when the object has synchronized method code. When
we enter a synchronized non-static method, we automatically acquire the lock
associated with the current instance of the class whose code we're executing (the
this instance).


does this means that if an object does not have synchronized methods, we can't acquire lock on that object. if we can acquire the lock , then how we do that ?

according to me the answer is yes, we can acquire lock on any object in java whether it has synchronized methods or not. we can do so using synchronized blocks. please somebody confirm if this is right ???

 
Mike Smyth
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your correct, you can think of the synchronized keyword on a method as simply being like having a synchronised block on the particular object say using "this" surrounding the entire method. I suspect that the book is getting at the fact that you don't need to synchronize on any object if you use the synchronized keyword for a method and by doing this it uses the "built-in lock".

Hope this helps,
Mike
 
Helen Ma
Ranch Hand
Posts: 451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I think you can lock on any object by using synchronized(obj) {...} , does not matter the object has syn method or not.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic