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
posted
0
[ September 18, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand
Joined: Mar 05, 2003
Posts: 1391
posted
0
Marlene Miller
Ranch Hand
Joined: Mar 05, 2003
Posts: 1391
posted
0
Barkat Mardhani
Ranch Hand
Joined: Aug 05, 2002
Posts: 787
posted
0
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
0
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
0
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.