ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
#### 两个线程交替打印A和B 具体代码: ~~~ public class ThreadApp { private static int NUM = 10; private static Object lock = new Object(); public static void main(String[] args) { new Thread(() -> { try { for (int i = 0; i < NUM; i++) { synchronized (lock) { lock.wait(); System.err.println("B"); lock.notify(); } } } catch (Exception e) { e.printStackTrace(); } System.err.println("end B"); }).start(); new Thread(() -> { try { for (int i = 0; i < NUM; i++) { synchronized (lock) { System.err.println("A"); lock.notify(); lock.wait(); } } } catch (InterruptedException e) { e.printStackTrace(); } System.err.println("end A"); }).start(); } } ~~~