wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Basic thread 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 "Basic thread doubt" Watch "Basic thread doubt" New topic
Author

Basic thread doubt

Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
a) When a thread calls a wait(), does that release all the locks it holds or just the object on which it has called wait()? i.e. if a thread calls a synchronized method on objA (class A) and then from there it calls a synchronized method on objB (class B) and then calls wait()? At that time whether it releases the locks for objA and objB both or just objB?
b) When a thread is in wait() state and is interrupted and it throws InterruptedException, does it relases the lock? My answer is No.
c) When a thread throws an uncaught exception inside a synchronized method of a class, and so before the controls leave the current method and the exception is propagated to the next level, does the thread releases all the locks ? My answer is Yes.
I know many of you may feel these questions to be very silly but sometimes such silly questions in the exam look lot more difficult.
Thanks,
Deep
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391

[ September 18, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
posted by Deep:
a) When a thread calls a wait(), does that release all the locks it holds or just the object on which it has called wait()? i.e. if a thread calls a synchronized method on objA (class A) and then from there it calls a synchronized method on objB (class B) and then calls wait()? At that time whether it releases the locks for objA and objB both or just objB?

The answer is "just the object on which it has called wait()"
Here is an example:
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
posted by Deep:

b) When a thread is in wait() state and is interrupted and it throws InterruptedException, does it relases the lock? My answer is No.

When a thread is in wait state, it has alread given up lock on the object
on which wait() was called.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
posted by Deep:

c) When a thread throws an uncaught exception inside a synchronized method of a class, and so before the controls leave the current method and the exception is propagated to the next level, does the thread releases all the locks ? My answer is Yes.

I would say Yes too. Here is an example:
 
wood burning stoves
 
subject: Basic thread doubt
 
Similar Threads
Avoding deadlocks
wait() releases all locks?
Island of Isolation
Looking for Java pattern
Question about Sequence Diagram