• 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

About synchoronized code blocks in java

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can we use a synchoronized code block to lock a third-party object? Please give details with examples!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic