六、线程的等待和唤醒
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(); } } } }