• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Basic thread doubt

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
reply
    Bookmark Topic Watch Topic
  • New Topic