华为多线程面试题解法二(sync_wait_notify) package src.main.java.com.qqjx.thread; /* * @Auther wangpeng * @Date 2021/2/2 */ public class T06_00_sync_wait_notify { public static void main(String[] args) { final Object o = new Object(); char[] aI = "1234567".toCharArray(); char[] aC = "ABCDEFG".toCharArray(); new Thread(()->{ synchronized (o) { for(char c : aI) { System.out.print(c); try { o.notify(); //叫醒其他线程 不会出让锁 o.wait(); // 线程阻塞 让出锁 } catch (InterruptedException e) { e.printStackTrace(); } } o.notify(); //必须,否则无法停止程序 } }, "t1").start(); new Thread(()->{ synchronized (o) { for(char c : aC) { System.out.print(c); try { o.notify(); o.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } o.notify(); } }, "t2").start(); } } 1 A 2 B 3 C 4 D 5 E 6 F 7 G