| Author |
wait() and notify()
|
Faraah Dabhoiwala
Greenhorn
Joined: May 30, 2007
Posts: 21
|
|
which object should be marked synchronized when we want a thread to wait??
how does a thread know which object amongst those which are waiting to notify??
|
<b>Please don't visit my homepage...</b><br /> <a href="http://shadabworld.110mb.com" target="_blank" rel="nofollow">http://shadabworld.110mb.com</a>
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
For first question, see my response here...
http://www.coderanch.com/t/233995/Threads-Synchronization/java/synchronization
For second question, most modern JVMs pass the threading to the underlying OS libraries. So it is the OS libraries that choose the waiting thread. As for the Java Specification, it doesn't require any order or preference.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Faraah Dabhoiwala
Greenhorn
Joined: May 30, 2007
Posts: 21
|
|
didnt get what you wrote there
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Shadab Wadiwala wrote:didnt get what you wrote there 
It looks like you reworded you question, after I answered it. I read the previous question as why synchronization was needed.
Henry
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
which object should be marked synchronized when we want a thread to wait??
The object that will be used to wait, should be the object that needs to be synchronized.
Henry
|
 |
Balraj Momi
Ranch Hand
Joined: Jul 23, 2009
Posts: 45
|
|
First of all ,you should know that objects don't wait for threads, Its opposite. Threads actually wait for an notification so that they can continue their operation. One thread can own multiple locks but one object lock can only be owned by one thread.
Let me put it this way, following is an example where a machine is waiting for a for a job to work on. Here job is a object and as soon as a new job is build, machine will get notification to continue to work on that job.
Though all of you may agree that, this code is not foolproof, for example notify can happen even before wait starts. This is just to help Shadab understand how threads owns lock on an object and gets notified to reclaim their property .
Regards
|
Regards
Balraj Kumar
SCJP 5 95%
SCWCD 82%
Preparing for SCBCD
|
 |
 |
|
|
subject: wait() and notify()
|
|
|