File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes // object  lock  doubt 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 "// object  lock  doubt" Watch "// object  lock  doubt" New topic

// object lock doubt

Meg Adal
Ranch Hand

Joined: Mar 12, 2002
Posts: 70
In Exam Cram book they have stated like this:

A single thread can obtain locks on many objects and/or multiple locks on same object

I think last part of statement is incorrect.
I guess as there is only one lock with given object ,thread can't obtain multiple locks on same object
correct me if I am wrong

Thanks in advance
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Imagine that the methods are protected by different locks as it follows:
method1() { synchronized(lock1) {method2();} }
method2() { synchronized(lock2) {} }
A thread calling method1 will obtain two locks (if available)

SCJP2. Please Indent your code using UBB Code
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Also, see this link.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
But those are two locks on two different objects (lock1 and lock2).
I think the statement really means that the same thread can lock the same object more than once, so this code won't deadlock:

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Hi all,
I think it's more like re-acquiring the lock of an object than obtaining multiple locks of the same object. This is to prevent instances like a thread waiting for a lock that it already has.
I agree. Here's the link:
subject: // object lock doubt
It's not a secret anymore!