Results 1 to 1 of 1
- 03-30-2009, 05:33 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
threads waiting the same event :searching for a specific notification
hi all,
i have two thread waiting an event
and another who notify them
the two thread could not both see the event only one could see the notification
1st casePHP Code://the class event public final class Event { /** * variable used to store the notification if it takes place * before being expected */ private boolean status = false; public final synchronized void waitForNotification() { while (status == false ) { try { wait(); } catch (InterruptedException e) { } } status = false ; } // notify all waits on this event public final synchronized void wakeUp() { status = true; notifyAll(); } } // the class TestThread implements a thread that have an event waiting for a notification public class TestThread extends Thread{ private Event event; public TestThread( Event e){ event=e; } public void run(){ for (int i = 1; i < 4; i += 1) { try { Thread.sleep(2); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } e.waitForNotification(); } } } // the class TestThread2 implements a thread that have an event waiting for a notification public class TestThread2 extends Thread{ private Event event; public TestThread( Event e){ event=e; } public void run(){ for (int i = 1; i < 4; i += 1) { e.wakeUp(); } } } //the class main public class test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Event e = new event(); TestThread th1 = new TestThreadConsumer(e); TestThread th2 = new TestThread(e); TestThread2 th= new TestThread2(e); th1.start(); th2.start(); th3.start(); } }
the problem is that the notification could not be seen to the tow threads
because if the tow are waiting the notification
and the other thread send notification
one of the tow see the notificztion and wake up and set the flag status to false so the other one could not ever wake up
2d case
the notification of the second thread could be done in the second itiration or in the other ones
please could u answer and help me
thank you
Similar Threads
-
JFrame created but window contents not showing, othr threads keep waiting on the user
By FezKazi in forum AWT / SwingReplies: 1Last Post: 02-20-2009, 03:49 PM -
how do i print a specific txt file on a specific printer
By nikhilbhat in forum New To JavaReplies: 2Last Post: 11-08-2008, 10:40 AM -
Once again: waiting in a thread loop.
By willemjav in forum Threads and SynchronizationReplies: 115Last Post: 09-22-2008, 01:35 PM -
Waiting for a button to be pressed
By SomeGuyOverThere in forum New To JavaReplies: 6Last Post: 08-21-2008, 09:30 PM -
waiting for a file
By Fleur in forum New To JavaReplies: 2Last Post: 06-23-2008, 08:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks