0

I have the following code:

public class ThreadA {
public static void main(String[] args){
    ThreadB b = new ThreadB();
    b.start();

    synchronized(b){
        try{
            b.wait();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}}


class ThreadB extends Thread{
@Override
public void run(){
    synchronized(this){
        notify();
    }
}}

I'm pretty new to wait/notifyThreads and I need to find a way to wait before the notify() of Thread B until I call it explicitly from another class, preferably at first from a test case, later on from detached web service class. I don't get it, can you please help me out?

Kalamarico
  • 5,466
  • 22
  • 53
  • 70
MWO
  • 2,627
  • 2
  • 10
  • 25

2 Answers2

0

If you want to wait for a task to be completed, I suggest using Java Concurrency API way:

public class WaitATaskExample {
  public static void main(String[] args) {
    ExecutorService service = null;
    try {
      service = Executors.newSingleThreadExecutor();
      Future<?> future = service.submit(() -> {
        // your task here
        Thread.sleep(5000);
        return null;
      });
      try {
        future.get(); // blocking call
      } catch (InterruptedException | ExecutionException e) {
        // handle exceptions
      }
    } finally {
      if (service != null) {
        service.shutdown();
      }
    }
  }
}

Another approach using CountDownLatch:

public class WaitATaskExample {
  public static void main(String[] args) {
    ExecutorService service = null;
    try {
      service = Executors.newFixedThreadPool(2);
      CountDownLatch latch = new CountDownLatch(1);

      Callable<Object> waitingTask = () -> {
        latch.await(); // wait
        return null;
      };
      Callable<Object> notifier = () -> {
        Thread.sleep(2_000);
        latch.countDown(); // notify
        return null;
      };

      service.submit(waitingTask);
      service.submit(notifier);
    } finally {
      if (service != null) {
        service.shutdown();
      }
    }
  }
}
Maxim Blumental
  • 763
  • 5
  • 26
0
import java.lang.InterruptedException;

public class ThreadRunner {
    public static void main(String[] args){
        ThreadA a = new ThreadA();
        ThreadB b = new ThreadB(a);

        b.start();
        try {
            Thread.sleep(1000);
        } catch(InterruptedException e) {}

    }
}

class ThreadA extends Thread {
    String name = "threadA";

    public void run() {
        try {
            synchronized (this) {
                wait();
            }
            System.out.println(name + " " + "notified!");
        } catch(InterruptedException e) {
           // TODO: something
        }
    }
}

class ThreadB extends Thread {
    ThreadA a;
    String name = "threadB";

    public ThreadB(ThreadA a) {
        this.a = a;
    }

    @Override
    public void run(){

        a.start();
        try {
            Thread.sleep(1000);
        } catch(InterruptedException e) {}
        synchronized (a) {
            System.out.println(name + " " + "trying to notify A!");
            a.notify();
        }

    }
}
Justin Thomas
  • 5,680
  • 3
  • 38
  • 63
  • Can you please tell me why you need the Thread.sleep(1000); in ThreadB? If you remove it, a won't get notified. In Threadrunner I can remove it and it still works... – MWO Jan 05 '18 at 19:26
  • Actually I mean the whole try catch block, when you remove it it won't work. – MWO Jan 05 '18 at 19:39
  • The sleep is ensuring that thread A has actually started and is waiting. The proper way to do that would be to have a notify b then b notify a. – Justin Thomas Jan 08 '18 at 14:25