notify 的使用 Posted on 2021-02-13 1234567891011121314151617181920212223242526272829303132333435public class NotifyTest { public synchronized void testWait(){ System.out.println(Thread.currentThread().getName() +" Start-----"); try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() +" End-------"); } public static void main(String[] args) throws InterruptedException { final NotifyTest test = new NotifyTest();// 创建五个线程取运行对象的方法 for(int i=0;i<5;i++) { new Thread(new Runnable() { @Override public void run() { test.testWait(); } }).start(); } //通知一个 线程 synchronized (test) { test.notify(); } Thread.sleep(3000); System.out.println("-----------分割线-------------"); //通知所有线程 synchronized (test) { test.notifyAll(); } }} Donate comment here Donate WeChat Pay