Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Thread doubt

 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question:

Thread Z holds the lock on object A. Thread X is blocked inside a wait call on ObjectA.

What allows thread X to become runnable?
A. Thread X is interrupted.
B. Thread X is interrupted.
C. Thread X�s wait() times out.
D. Thread Z calls Thread.sleep(100);
E. Thread Z releases the lock on A and calls the notify() method on thread X
F. Thread Z releases the lock on A and calls the notifyAll() method on objectA.
Answer given: F

I thought it would of been C and F???

Can anyone clarify this?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marzo,

A and B has same text, but I'm thinking A,C and F instead just F.

I hope someone would clarify our understanding.
 
Marcelo Ortega
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh sorry thats a typo.
 
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marzo,

Thread Z holds the lock on object A. Thread X is blocked inside a wait call on ObjectA.

C. Thread X�s wait() times out.

No, as even if X's wait times out X remains in non runnable state because it must acquire lock on object A which is hold by thread Z, after acquiring lock on object A, thread X can go to runnable state.

Other example is say when we call notify on object A in thread Z, now thread waiting for object lock (Thread X) does not go directly to Runnable it must wait and acquire lock on object A than only it can go to runnable.

(calling notify does not immediately release lock, only after synchronized method or block completes than only lock is released on object A by thread Z, now X thread can acquires lock and can make transition to runnable state).

Hope this helps,
bye.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic