Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object lock

 
cybel sheriden
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
iam i correct in stating these 3 statement.

1> when u synchronize a method, lock of object used to invoke this method should be acquired.


2>
method()
{ synchronize(this) { }
}
above code lock is obtained on object used to invoke the method



3>
method()
{
synchronize(a){}
}
in the above code lock is obtained on object refered by 'a' and of the object used to invoke the method, so 2 locks obtained
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 yes
2 yes
3 no, only the lock on a is held
 
cybel sheriden
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if in the third case the lock is obtained on only object ref by 'a',then as per k&b page 523 "third party object lock for synchonizing a piece of code gives ability to have more then one lock for code synchronisation within a single object".
so i assumed tht 2 locks are obtained for the 3rd code sample.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Cybel, I think you talk about this:
 
James Carman
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by George Bolyuba:
Hi

Cybel, I think you talk about this:


In this case, there is only one lock obtained. Since the executing thread already has a lock on the object "a", the second synchronized block really does nothing for you.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No lock is obtained at all as the code won't even compile...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic