0

I would like my main thread to notify another thread upon a given event, but without pausing itself. The other thread is an infinite loop, and I need it to wait after each iteration, until the main thread wakes it again.

This seems to rule out the wait/notify pattern as it does pause the thread that calls notify on the shared monitor. I also thought about CyclicBarrier but I do not want the main thread to call Barrier.await, and wait until the other thread calls Barrier.await as well, because it can take a long time...

Any idea ? Thanks !

  • Somehow screams [Semaphore](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html) to me: Background thread decrements semaphore, waits if zero, main thread increments semaphore. No waits on the main thread involved. Can you elaborate more on the problem? – dhke Aug 05 '16 at 08:46
  • Possible duplicate of [How do I pause Threads properly with wait() and notify()](http://stackoverflow.com/questions/26764650/how-do-i-pause-threads-properly-with-wait-and-notify) – sheepiiHD Aug 05 '16 at 09:15

2 Answers2

0

make the waiting thread like this:

class MyThread extends Thread() {
private volatile boolean go;

public void wakeUpBuddy() {
    go=true;
    synchronized(this) {
      notify();
    }
}

public void run() {

   while(!interrupted()) {
     // some work before wait

     synchronized(this) {
       while(!go) {
         wait();
       }
       go = false;
     }

     // some work after release
   }
}


}

Then from the main thread call on the instance of MyThread.wakeUpBuddy(); and it will go one pass and wait for another call.

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
0

How about using Observer pattern?

import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.TimeUnit;

public class Main extends Observable {

    public static void main(String... args) {

        final MyThread t = new MyThread();

        final Main m = new Main();
        m.addObserver(t);

        // start the thread
        t.start();

        for (int i = 0; i < 25; i++) {
            if (i % 5 == 0) { // event received?
                m.setChanged();
                m.notifyObservers(i);
            }

            try {
                Thread.sleep(TimeUnit.SECONDS.toMillis(1));
            } catch (Exception ex) {

            }
        }

    }

}

class MyThread extends Thread implements Observer {

    private boolean wait = true;

    @Override
    public void run() {

        while (true) {

            while (wait) {
                try {
                    Thread.sleep(TimeUnit.SECONDS.toMillis(1));
                } catch (InterruptedException ex) {
                    // interrupted
                }
            }

            System.out.println("I am invoked ..");
            wait = true;
        }

    }

    @Override
    public void update(Observable o, Object arg) {
        System.out.println("Update received .. " + arg);
        wait = false;
    }

}
Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33