Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic thread doubt

 
Deep Chand
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ September 18, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic