Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what to answer with the yesterdays discussion on join method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what to answer with the yesterdays discussion on join method" Watch "what to answer with the yesterdays discussion on join method" New topic
Author

what to answer with the yesterdays discussion on join method

NareshAnkuskani Kumar
Ranch Hand

Joined: Sep 15, 2005
Posts: 76
so what should be the answer if i face a question like among the following methods which of them releases the lock on the object?
join()
sleep()
notify()
wait()
vandu matcha
Ranch Hand

Joined: Dec 28, 2005
Posts: 57
i think it is notify which releases the lock...(notifyall as well ...)
correct me if i am wrong
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108
The wait() method releases the lock.

from the api docs for the wait() method:

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.


_M_


Mike Noel
Veer Batra
Ranch Hand

Joined: Mar 12, 2001
Posts: 35
join(): join() can't release the lock as join is to keep the current thread going till the thread dies. So after that lock may be available.
sleep(): sleep() can realease the lock as sleep put the thread in sleep mode and releases all resources related to CPU.
notify(): just notifies and can hold the lock even after, but it is upto the notified thread to see if lock is avalable and secure it. Otherwise, if the thread who notified but still did not relaese the lock then notified thread/ready thread remain blocked until lock is available.
wait() : wait should release the lock as soon as it goes in the wait mode.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please do not duplicate threads.
Responses here, please.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Veer Batra:
join(): join() can't release the lock as join is to keep the current thread going till the thread dies. So after that lock may be available.


The whole point of the linked thread is that join() does release the lock on the Thread object, because it calls wait() on it. So this is wrong.


sleep(): sleep() can realease the lock as sleep put the thread in sleep mode and releases all resources related to CPU.


A common misperception, I think, but wrong. sleep() has absolutely no effect on an object's monitor. If you sleep while holding a lock on an object, then no one else can lock that object until you wake up and release the lock.


notify(): just notifies and can hold the lock even after, but it is upto the notified thread to see if lock is avalable and secure it. Otherwise, if the thread who notified but still did not relaese the lock then notified thread/ready thread remain blocked until lock is available.


This is correct, although it would have been much simpler to just say "has no effect on locked state."


[Jess in Action][AskingGoodQuestions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what to answer with the yesterdays discussion on join method
 
Similar Threads
inner join Vs outer join
Question on Thread
can i get an example which illustrate the use of join() method
concept of join in thread
Regarding Join method in Threads