面试题:有A,B,C三个线程,如何保证三个线程同时执行?如何在并发情况下保证三个线程依次执行?如何保证二个线程有序交错进行?
面试题:有A,B,C三个线程,如何保证三个线程同时执行?如何在并发情况下保证三个线程依次执行?如何保证二个线程有序交错进行?
这三个题目都是控制多个线程执行的顺序。这一类题目除了通过一些判断来完成,大多情况下是通过锁来控制顺序的。下面我们就分别来讨论下这三个题目的实现办法。
有A,B,C三个线程,如何保证三个线程同时执行?
保证线程同时执行可以用于并发测试。可以使用倒计时锁CountDownLatch
实现让三个线程同时执行。代码如下所示:
ExecutorService executorService = Executors.newCachedThreadPool();
CountDownLatch countDownLatch = new CountDownLatch(1);
executorService.submit(()->{
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程A执行,执行时间:" + System.currentTimeMillis());
});
executorService.submit(()->{
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程B执行,执行时间:" + System.currentTimeMillis());
});
executorService.submit(()->{
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程C执行,执行时间:" + System.currentTimeMillis());
});
countDownLatch.countDown();
打印内容如下,通过时间可以证明三个线程是同时执行的。
线程A执行,执行时间:1617811258309
线程C执行,执行时间:1617811258309
线程B执行,执行时间:1617811258309
当然让三个线程同时进行,也可以使用循环栅栏CyclicBarrier
来实现,当三个线程都到达栅栏处,才开始执行。
有A,B,C三个线程,如何在并发情况下保证三个线程依次执行?
要保证三个线程依次执行,就是说,A线程执行完之后,才执行B线程,B线程执行完之后才执行C线程。
如下代码所示,分别用两个方法实现了分别用volatile
和倒计时锁CountDownLatch
实现顺序执行。
package com.wangguitang.freedom.interview.concurrent;
import org.junit.jupiter.api.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 让ABC三个线程循序打印
*
* @author freedom wang
* @date 2021-04-08 22:48:33
*/
public class ThreeThreadsOrderPrint {
private volatile int count = 0;
/**
* 使用一个变量进行判断执行哪个线程。没有轮到的线程在不停循环,没有停止线程。
*
* @author freedom wang
* @date 2021-04-08 22:57:29
*/
@Test
public void testUseVolatile() {
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.submit(() -> {
while (true) {
if (count == 0) {
for (int i = 0; i < 10; i++) {
System.out.println("A - " + i);
}
count = 1;
break;
}
}
});
executorService.submit(() -> {
while (true) {
if (count == 1) {
for (int i = 0; i < 10; i++) {
System.out.println("B - " + i);
}
count = 2;
break;
}
}
});
executorService.submit(() -> {
while (true) {
if (count == 2) {
for (int i = 0; i < 10; i++) {
System.out.println("C - " + i);
}
count = 3;
break;
}
}
});
}
/**
* 使用倒计时锁来进行控制
*
* @author freedom wang
* @date 2021-04-08 22:57:56
*/
@Test
public void testUseCountDownLatch() {
ExecutorService executorService = Executors.newCachedThreadPool();
CountDownLatch aLatch = new CountDownLatch(1);
CountDownLatch bLatch = new CountDownLatch(1);
CountDownLatch cLatch = new CountDownLatch(1);
executorService.submit(() -> {
try {
aLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
System.out.println("A - " + i);
}
bLatch.countDown();
});
executorService.submit(() -> {
try {
bLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
System.out.println("B - " + i);
}
cLatch.countDown();
});
executorService.submit(() -> {
try {
cLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
System.out.println("C - " + i);
}
});
aLatch.countDown();
}
}
有A,B,C三个线程,如何保证三个线程有序交错进行?
实现三个线程交错打印,可以使用ReentrantLock以及3个Condition来实现,代码如下所示:
package com.wangguitang.freedom.interview.concurrent;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* 实现三个线程交替打印
*
* @author freedom wang
* @date 2021-02-22 21:42:44
*/
public class ThreeThreadsAlternatePrint {
private static final ReentrantLock lock = new ReentrantLock();
private static Condition c1 = lock.newCondition();
private static Condition c2 = lock.newCondition();
private static Condition c3 = lock.newCondition();
/**
* 解题思路:使用显示锁
*
* @author freedom wang
* @date 2021-02-22 21:47:36
*/
public static void main(String[] args) {
new Thread(() -> {
try {
lock.lock();
for (int i = 0; i < 10; i++) {
System.out.println("A - " + i);
c2.signal();
c1.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}).start();
new Thread(() -> {
try {
lock.lock();
for (int i = 0; i < 10; i++) {
System.out.println("B - " + i);
c3.signal();
c2.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}).start();
new Thread(() -> {
try {
lock.lock();
for (int i = 0; i < 10; i++) {
System.out.println("C - " + i);
c1.signal();
c3.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}).start();
}
}