Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default 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
    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 es) {
    // TODO Auto-generated catch block
    es.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();
    th.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

  2. #2
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    my question is to find a sollution to make my application woks well
    i wanna to make all the thread be notified all
    here i have this problem only one thread could be notified

  3. #3
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    what ?
    i don't understand
    i had 3 run methods
    the problem i had a live lock here
    because the notification is seen by just one thread
    and the other one is waiting because it didn't see the notification done by the thread th

Similar Threads

  1. threads waiting the same event :searching for a specific notification
    By nabila.abdessaied in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-30-2009, 05:33 PM
  2. Replies: 1
    Last Post: 02-20-2009, 03:49 PM
  3. Replies: 2
    Last Post: 11-08-2008, 10:40 AM
  4. Waiting for a button to be pressed
    By SomeGuyOverThere in forum New To Java
    Replies: 6
    Last Post: 08-21-2008, 09:30 PM
  5. waiting for a file
    By Fleur in forum New To Java
    Replies: 2
    Last Post: 06-23-2008, 08:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •