private static void m5_1() throws InterruptedException {
int i = 0;
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
LockSupport.park();
System.out.println(1);
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
LockSupport.park();
System.out.println(2);
}
}
});
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
LockSupport.park();
System.out.println(3);
}
}
});
thread1.start();
thread2.start();
thread3.start();
while (true) {
if (i % 3 == 0) {
LockSupport.unpark(thread1);
} else if (i % 3 == 1) {
LockSupport.unpark(thread2);
} else if (i % 3 == 2) {
LockSupport.unpark(thread3);
}
i++;
Thread.currentThread().sleep(1000);
}
}
有 3 个独立的线程,一个只会输出 A,一个只会输出 L,一个只会输出 I。 在三个线程同时启动的情况下,请用合理的方式让他们按顺序打印 ALIALI。
最新推荐文章于 2023-02-28 14:56:38 发布