六、线程的等待和唤醒

 

1.wait()和notify()的简单示范

public class Wait extends Thread{
    public synchronized  void run()
    {
        System.out.println(getName()+"执行notify()");
        notify();

    }
    public static void main(String []args) {
        Wait w = new Wait();
        synchronized (w){
        try {
            w.start();
            System.out.println(Thread.currentThread().getName() + "睡眠");
            Thread.sleep(3000);

            System.out.println(Thread.currentThread().getName() + "等待");
            w.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    }
}
View Code

相关文章:

  • 2022-02-13
  • 2022-12-23
  • 2021-07-31
  • 2022-12-23
  • 2021-11-29
  • 2021-07-28
猜你喜欢
  • 2022-01-16
  • 2022-12-23
  • 2022-01-08
  • 2021-05-25
  • 2021-12-14
  • 2021-10-23
  • 2021-07-07
相关资源
相似解决方案