permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is my Understanding Correct ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is my Understanding Correct ?" Watch "Is my Understanding Correct ?" New topic
Author

Is my Understanding Correct ?

Sid Robin
Ranch Hand

Joined: Nov 24, 2007
Posts: 53
This para is taken from K & B

"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 as the lock, so you could, for example, use some third-party object as the lock for this piece of code. That gives you the ability to have more than one lock for code synchronization within a single object."

class SyncTest {
public void doStuff() {
System.out.println("not synchronized");
synchronized(this) {
System.out.println("synchronized");
}
}
}



Does this para mean to say that if we replace "this" operator with any third party object, then we will have two locks

One is the lock of the object which was used to invoke the method and
the other was the third party object

Am i correct ?

Regards
sid
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18497
    
  40

Does this para mean to say that if we replace "this" operator with any third party object, then we will have two locks


You will have one lock -- you will replace the lock from the "this" object with the lock from another object.

One is the lock of the object which was used to invoke the method and
the other was the third party object


There is no lock need to invoke the method -- the method isn't synchronized. (which BTW, if the method is syschronized, it would have used the "this" object)

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is my Understanding Correct ?
 
Similar Threads
thread doubt
Thread
Difference between synchronized method and synchrozed block
method Vs block lever synchronization.KATHY I NEED UR COMMENTS TOO
Thread