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 wait() and lock 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 "wait() and lock" Watch "wait() and lock" New topic
Author

wait() and lock

uzma Akbar
Ranch Hand

Joined: Sep 21, 2005
Posts: 40
Hey guys,

First of all I should thank you once agian for helping me in the problems I posted in the past.

Here I am dealing with one confusion in threads and want help and appreciate it in advance if somebody assists.

When we call wait method on the object then only the lock of that Object is released(Khalid Mughal's one of the query)

Select the two correct answers.

a) A thread can hold more than one lock at a time.

b) Invoking wait() on a Thread object will relinquish all locks held by the thread.

c)Invoking wait() on an object whose lock is held by the current thread will relinquish the lock.

d)Invoking notify() on a object whose lock is held by the current thread will relinquish the lock.

e)Multiple threads can hold the same lock at the same time.

Answer a,c

I was doing the Master test from K&B book CD, one of the answer is:

When a thread is waiting as a result of wait(), it releases all the locks it holds. while option c above contradicts it.

Please help

Uzma
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

When a thread is waiting as a result of wait(), it releases all the locks it holds. while option c above contradicts it.


Obviously, this statement is untrue, as it will make the wait() method pretty dangerous to use -- free locks used by Swing, JDBC, etc.

However, the book may be referring to something else. A thread is allowed to acquire a lock that it already owns. This is needed for a synchronized method to call another synchronized method of the same instance. The wait() method will release the lock completely, meaning all the times it acquired it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
uzma Akbar
Ranch Hand

Joined: Sep 21, 2005
Posts: 40
Thanks Wong for clearing up my concept.

The test I was doing from the CD comes with the K&B book so was wondering as my concept was not going side by side. If you have an access to CD then please check what I had mentioned in the Master exam.

Thanks again!
Uzma
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait() and lock
 
Similar Threads
Calling wait() when we have obtained two or more locks
wait()
Dan's: Blocked vs Waiting - possible mistake
Thread Question??
A few questions on wait(), sleep(int) and InterruptedException