File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About synchoronized code blocks in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About synchoronized code blocks in java" Watch "About synchoronized code blocks in java" New topic
Author

About synchoronized code blocks in java

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

How can we use a synchoronized code block to lock a third-party object? Please give details with examples!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
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


Ping & DNS - my free Android networking tools app
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
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

Joined: Nov 04, 2009
Posts: 2066

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
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
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

Joined: Jun 21, 2008
Posts: 423
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About synchoronized code blocks in java