【问题标题】:Java wait() notify()Java 等待() 通知()
【发布时间】:2018-01-04 23:14:47
【问题描述】:

我有以下代码:

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();
    }
}}

我对 wait/notifyThreads 很陌生,我需要找到一种方法在线程 B 的 notify() 之前等待,直到我从另一个类显式调用它,最好首先从测试用例调用,然后从分离网络服务类。没看懂,能帮帮我吗?

【问题讨论】:

标签: java multithreading wait notify


【解决方案1】:
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();
        }

    }
}

【讨论】:

  • 你能告诉我为什么你需要 Thread.sleep(1000);在线程 B 中?如果您将其删除,则不会收到通知。在 Threadrunner 中我可以删除它,它仍然可以工作......
  • 其实我的意思是整个try catch块,当你删除它时它就不起作用了。
  • 睡眠确保线程 A 已实际启动并正在等待。正确的做法是先通知 b,然后通知 b。
【解决方案2】:

如果你想等待一个任务完成,我建议使用 Java Concurrency API 方式:

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();
      }
    }
  }
}

使用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();
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多