public class ThreadTest {
private static int count = 1;
public static void main(String[] args) throws InterruptedException {
Object obj = new Object();
Runnable runnable = () -> {
while (true) {
synchronized (obj) {
obj.notify();
if (count > 100) {
break;
}
System.out.println(Thread.currentThread().getName() + " " + count);
count++;
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
Thread.sleep(100_000);
}
}
多线程交替打印数字
最新推荐文章于 2024-07-26 14:49:26 发布