Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

question on wait() from K&B master exam

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi well since i m likely to take my exam on next tuesday, today i took the K&B master exam which comes on the accompanying CD.
Well i got only 2 ques wrong i.e. my score was 59/61. 1 was a foolish mistake but the other question is the one which i think was ambiguous.
the question was a to choose 2 correct statements which r true.
the ans were:
1.notifyAll() must be called from within synchronized code.
2.when the wait() method is called the thread releases all its locks.
well i got the 1st one right but I think the second answer given is wrong.
Bcoz when the wait method is called on an object, the thread releases only the lock for that object
please tell me if i m right or the ans given is right
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, what happens when a thread holding the lock on an object goes to the wait state? Is this possible?
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe i have heard Mohit statement somewhere before and i think he is correct with his assumption.
I am not sure about this but if the thead in the waiting pool still have locks for objects, then this will extremely increase the chance of a deadlock to happen. Am i right?
And on the other hand, what is the point of having the lock of an object if it is in the waiting pool?
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still think that Mohit is correct. Threads releases their locks on object when the wait() method is called.
 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vicken Karaoghlanian:
I still think that Mohit is correct. Threads releases their locks on object when the wait() method is called.



Thread release the lock and enter wait state. Another thread acquires the lock. That thread has to invoke notify/notifyAll so that the first thread which is waiting gets a chance to execute.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Pradeep


Thread release the lock and enter wait state. Another thread acquires the lock. That thread has to invoke notify/notifyAll so that the first thread which is waiting gets a chance to execute.


This statement is correct, I totally agree with it. Dislike your old post which you deleted.
 
Pradeep bhatt
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dislike your old post which you deleted.


It has 3 years since I have actually used Threads in real life. Yes, I did study threads for SCJP but not much.
That is why Javaranch is great , nice way to brush various technologies and mistakes are all caught.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Pradeep


It has 3 years since I have actually used Threads in real life. Yes, I did study threads for SCJP but not much.
That is why Javaranch is great , nice way to brush various technologies and mistakes are all caught.


Cheers man....
[ December 08, 2003: Message edited by: Vicken Karaoghlanian ]
 
Mohit Goyal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi well when the thread holds locks on multiple objects it only releases locks on the object on which the wait() method is called.
the ans given in the exam suggests that it releases all the locks. which is not correct as given in the book Core Java 2 Advanced topics by Cay Horstmann in Ch1
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The given answer is wrong.


wait() method releases the lock on the current object only. If it has also acquired locks on some other objects, it does not release those locks.


[ December 08, 2003: Message edited by: Vicken Karaoghlanian ]
 
Pradeep bhatt
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what happens when a thread holding the lock on an object goes to the wait state


Yes it is possible.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe Mohit is right: a thread doesn't release locks on ALL objects if it goes into waiting state. Only the lock on the object wait() was called from would be released. Here's the code to solidify the concept:

This program never completes because the first running thread retains a lock on sb1 StringBuffer object even after being put in a waiting pool by the call to sb2.wait().
[ December 08, 2003: Message edited by: Vad Fogel ]
[ December 08, 2003: Message edited by: Vad Fogel ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic