Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About synchoronized code blocks in java

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can we use a synchoronized code block to lock a third-party object? Please give details with examples!
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What, exactly, do you mean by "lock a third-party object"? One uses an object as a lock for a synchronized code section. It doesn't matter what kind of object, it only matters that all threads that should be kept out of a synchronized section use the same lock object.

The Sun Java Tutorial, as usual, has more on this: http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's said in K&B ' s book:
When you synchronize a method, the object used to invoke the method is the object whose lock must be acquired. But when you synchronize a block of code, you specify which object's lock you want to use the lock, so you could, for example, use some third - party object as the lock for this piece of code.

Please explain this. Thanks in Advanced!
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that sentence got mangled a bit. What it should say instead of "you specify which object's lock you want to use the lock" is "you specify which object you want to use as the lock".

Do you understand what a lock is in principle? The tutorial I linked to explains how objects can be used as locks in Java code for synchronization purposes.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, may be. But, assume there is a method, we run that method with an object(take it as Obj1). Within that method, we synchronized a code block, then how can we lock a third object, since we are executing with our Obj1?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether the object that contains the code being run is related to locking depends on how you synchronize. Implicit synchronization -meaning declaring a method as synchronized- would use that object as lock. Explicit synchronization uses a "synchronized (someObject) { ... }" code block where "someObject" is the object used as lock. someObject could be whatever object you want; it need not be of the same class as the object containing the code that's being run. The tutorial explains all this.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:Ok, may be. But, assume there is a method, we run that method with an object(take it as Obj1). Within that method, we synchronized a code block, then how can we lock a third object, since we are executing with our Obj1?


Very simple. Synchronized blocks could be nested:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic