Forums Register Login

wait() and lock

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1202 times.
Similar Threads
Dan's: Blocked vs Waiting - possible mistake
wait()
Thread Question??
A few questions on wait(), sleep(int) and InterruptedException
Calling wait() when we have obtained two or more locks
wait() release all locks?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:49:34.