aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait & notify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait & notify" Watch "wait & notify" New topic
Author

wait & notify

Praveen Kumar Mathaley
Ranch Hand

Joined: Apr 14, 2003
Posts: 45
consider the following example two
threads wait, and there's only one notify , but both threads comes out of wait status
package thread;
class TestThread extends Thread
{
public void startMe()
{
synchronized(this)
{
notify();
System.out.println("Notified...");
try
{
System.out.println("waiting in startME");
wait();
System.out.println("woke from wait in startME");
}
catch(InterruptedException e)
{}
}
}
public void run()
{
try
{
synchronized(this)
{
System.out.println("waiting in run");
wait();
System.out.println("woke up from wait in run");
}
}
catch(InterruptedException e)
{}
}
public static void main(String[] args)throws Exception
{
TestThread t1 = new TestThread();
t1.start();
Thread.sleep(1000);
t1.startMe();
}
}

how does the single notify wakes up two threads that are waiting, it should be notifying only one thread
[ August 14, 2003: Message edited by: Praveen Kumar Mathaley ]
[ August 14, 2003: Message edited by: Praveen Kumar Mathaley ]
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
thanks for sharing the code. Is there any question?


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Praveen Kumar Mathaley
Ranch Hand

Joined: Apr 14, 2003
Posts: 45
my question is how does the single notify wakes up two threads that are waiting, it should be notifying only one thread and the other thread in wait state should wait untily notified
[ August 14, 2003: Message edited by: Praveen Kumar Mathaley ]
[ August 14, 2003: Message edited by: Praveen Kumar Mathaley ]
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
This is my guess:
When the run() method of the thread completes, an implicit call to notifyall() is made by the instance of the thread. I modified the code to prove this:

In this version, there are now 3 threads - 2 of which will wait on an instance of t1. One of the thread, the one created from an instance of t1, will not wait but instead would sleep. If you would run this code, you would notice that when that thread (t1) exits its run method, the 2 remaining threads(main and t2) that is in the wait state, will also exit. This despite the fact that no notify() call is made in the code.
But if you would notify t1 to wait instead of sleep (don't forget to add synchronize), the whole program would wait forever.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Praveen, I do not know the answer to your question.
Here are 3 things I have learned by experimenting with variations of your example.
1. If *two* new threads are waiting, when the main thread calls notify and wait, all 3 threads wake up.
Thread t2 = new Thread(t1); // second thread call run on same object
t2.start();
2. If the main thread calls notify, then *releases* the lock, then *acquires* the lock again, then calls wait, only the new thread wakes up.
synchronized(this) { notify(); }
synchronized(this) { wait(); }
3. If the threads are waiting on some object other than the Thread object, only the new thread wakes up.
Object lock = new Object();
In startMe(), synchronized(lock) { lock.notify(); ... lock.wait() }
In run(), synchronized(lock) { lock.wait(); }
4. I looked in the Sun Java bug database. So far, I have not found anything.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
bad post. control-x
[ August 14, 2003: Message edited by: Marlene Miller ]
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
One thing that I noticed about the code is that it is quite similar to what the method join() is doing i.e. it synchronized on the instance of the executing thread and wait on it. The only difference is that join() does not call notify(), but this one does. So the question now is, how can join() get out of the wait state? That is why I suspect that there is an implicit call to notifyall() when the run() method completes.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Alton, You are correct! Good thinking! I did a test.
1. Here is what the Java Programming Language says about join. �with the understanding that the runtime system will invoke notifyAll when the thread actually terminates.�
2. And here is what Concurrent Programming in Java, Doug Lea says about join. �Terminating threads automatically perform notifications.�
3. I put the new thread to sleep for 5 seconds. Meanwhile, I added 10 threads to the wait set of the Thread object. After 5 seconds, the sleeping thread awoke and terminated and all of the other threads were removed from the wait set (automatically notified).

5 seconds elapse
Thread-1 terminating
Thread-2 awake
Thread-3 awake
Thread-4 awake
Thread-5 awake
Thread-6 awake
Thread-7 awake
Thread-8 awake
Thread-9 awake
Thread-10 awake
Thread-11 awake
[ August 14, 2003: Message edited by: Marlene Miller ]
Praveen Kumar Mathaley
Ranch Hand

Joined: Apr 14, 2003
Posts: 45
my Thanks to Alton and Marlene
[ August 14, 2003: Message edited by: Praveen Kumar Mathaley ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait & notify