💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
#### 两个线程交替打印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(); } } ~~~