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
PHP 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();
}
}
1st case
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