public class Three {
public static void main(String[] args) {
Object lock = new Object();
final int[] i = {1};
Thread thread1 = new Thread(
() -> {
while (true) {
synchronized (lock) {
if (i[0] > 100) break;
if ((i[0] + 2) % 3 == 0) {
System.out.println(Thread.currentThread().getName() +
"打印" + i[0]);
i[0]++;
lock.notifyAll();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
);
thread1.setName("线程1");
thread1.start();
Thread thread2 = new Thread(
() -> {
while (true) {
synchronized (lock) {
if (i[0] > 100) break;
if ((i[0] + 1) % 3 == 0) {
System.out.println(Thread.currentThread().getName() +
"打印" + i[0]);
i[0]++;
lock.notifyAll();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
);
thread2.setName("线程2");
thread2.start();
Thread thread3 = new Thread(
() -> {
while (true) {
synchronized (lock) {
if (i[0] > 100) break;
if (i[0] % 3 == 0) {
System.out.println(Thread.currentThread().getName() +
"打印" + i[0]);
i[0]++;
lock.notifyAll();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
);
thread3.setName("线程3");
thread3.start();
}
}
Java实现三线程轮流打印1-100
最新推荐文章于 2023-08-30 09:18:16 发布