Java线程同步synchronized wait notifyAll

it2022-05-05  102

public class Main { public static void main(String[] args) { Main main = new Main(); main.test(); } private void test() { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("唤醒等待线程"); synchronized (Main.class) { Main.class.notifyAll(); } } }).start(); synchronized (Main.class) { try { System.out.println("等待..."); Main.class.wait(); System.out.println("等待结束"); } catch (InterruptedException e) { e.printStackTrace(); } } } }

 

输出:

等待... 唤醒等待线程 等待结束

 


最新回复(0)