• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wait & notify

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for sharing the code. Is there any question?
 
Praveen Kumar Mathaley
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bad post. control-x
[ August 14, 2003: Message edited by: Marlene Miller ]
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my Thanks to Alton and Marlene
[ August 14, 2003: Message edited by: Praveen Kumar Mathaley ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic