原题描述
启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20….以此类推, 直到打印到75.
其实这里可以在每个线程中直接循环5次控制输出的结束,但是很多人对中断并不是很熟悉,所以我写成了中断的形式,希望可以加深对中断的理解。
而且这样更具通用性!
public class ProceedInsequence {
private int no = 1;
private final Lock lock = new ReentrantLock();
private final Condition con1 = lock.newCondition();
private final Condition con2 = lock.newCondition();
private final Condition con3 = lock.newCondition();
private int curNum = 1;
private void printNum() {
if (curNum > 75) {
Thread.currentThread().interrupt();
return;
}
for (int i = 0; i < 5; i++)
System.out.println(Thread.currentThread().getName() + " " + (curNum++));
}
public void process1() {
lock.lock();
try {
while (no != 1)
con1.await();
printNum();
no = 2;
con2.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void process2() {
lock.lock();
try {
while (no != 2)
con2.await();
printNum();
no = 3;
con3.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void process3() {
lock.lock();
try {
while (no != 3) con3.await();
printNum();
no = 1;
con1.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
final ProceedInsequence p = new ProceedInsequence();
new Thread(() -> {
while (!Thread.currentThread().isInterrupted())
p.process1();
}).start();
new Thread(() -> {
while (!Thread.currentThread().isInterrupted())
p.process2();
}).start();
new Thread(() -> {
while (!Thread.currentThread().isInterrupted())
p.process3();
}).start();
}
}
版权声明:本文为qq_23666815原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。