public static Object obj1 = new Object();
public static void printAB(){
Thread t1 = new Thread(() -> {
while (true){
synchronized (obj1) {
System.out.println("T1:A");
obj1.notify();
try {
Thread.sleep(1000);
obj1.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread t2 = new Thread(() -> {
while (true){
synchronized (obj1) {
System.out.println("T2:B");
obj1.notify();
try {
Thread.sleep(1000);
obj1.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t1.start();
t2.start();
}
两个线程交叉打印一个打印A一个打印B 循环打印?
最新推荐文章于 2023-08-22 15:42:41 发布