1.直接在方法里使用线程池,能实现异步,会继续执行主线程代码
public void threadTest(){
ThreadPoolExecutor executor = ThreadUtil.buildThreadPool(5, "tttt");
for (int i = 0; i < 5; i++){
executor.execute(new RunnableTest(i));
}
logger.info("都跑好了");
}
private class RunnableTest implements Runnable{
int i;
public RunnableTest(int i){
this.i = i;
}
@Override
public void run() {
ThreadUtil.threadWait(10000);
logger.info(i + "跑好了");
}
}
上述代码执行结果为:
2021-05-24 11:38:33,678 [http-nio-16100-exec-5] INFO c.y.s.b.c.controller.TestController [TestController.java : 28] - 都跑好了
2021-05-24 11:38:43,679 [tttt-4] INFO c.y.s.b.c.controller.TestController [TestController.java : 41] - 3跑好了
2021-05-24 11:38:43,680 [tttt-1] INFO c.y.s.b.c.controller.TestController [TestController.java : 41] - 0跑好了
2021-05-24 11:38:43,679 [tttt-5] INFO c.y.s.b.c.controller.TestController [TestController.java : 41] - 4跑好了
2021-05-24 11:38:43,680 [tttt-2] INFO c.y.s.b.c.controller.TestController [TestController.java : 41] - 1跑好了
2021-05-24 11:38:43,679 [tttt-3] INFO c.y.s.b.c.controller.TestController [TestController.java : 41] - 2跑好了
2.当需要等待多线程跑完,再继续执行主代码,则可以使用CountDownLatch
public void threadTest(){
ThreadPoolExecutor executor = ThreadUtil.buildThreadPool(5, "tttt");
CountDownLatch countDownLatch = new CountDownLatch(5);
for (int i = 0; i < 5; i++){
executor.execute(new RunnableTest(i, countDownLatch));
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
logger.info("出现异常", e);
}
logger.info("都跑好了");
}
private class RunnableTest implements Runnable{
int i;
CountDownLatch countDownLatch;
public RunnableTest(int i, CountDownLatch countDownLatch){
this.i = i;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
ThreadUtil.threadWait(10000);
logger.info(i + "跑好了");
} finally {
countDownLatch.countDown();
}
}
}
上述代码执行结果为:
2021-05-24 11:44:11,563 [tttt-2] INFO c.y.s.b.c.controller.TestController [TestController.java : 51] - 1跑好了
2021-05-24 11:44:11,563 [tttt-5] INFO c.y.s.b.c.controller.TestController [TestController.java : 51] - 4跑好了
2021-05-24 11:44:11,563 [tttt-4] INFO c.y.s.b.c.controller.TestController [TestController.java : 51] - 3跑好了
2021-05-24 11:44:11,564 [tttt-3] INFO c.y.s.b.c.controller.TestController [TestController.java : 51] - 2跑好了
2021-05-24 11:44:11,564 [tttt-1] INFO c.y.s.b.c.controller.TestController [TestController.java : 51] - 0跑好了
2021-05-24 11:44:11,564 [http-nio-16100-exec-7] INFO c.y.s.b.c.controller.TestController [TestController.java : 35] - 都跑好了
3.但需要返回值时,可以用Callable,但在主线程中用Future接收,会导致后续的主线程代码阻塞
public void threadTest() throws ExecutionException, InterruptedException {
ThreadPoolExecutor executor = ThreadUtil.buildThreadPool(5, "tttt");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++){
Future<String> future = executor.submit(new CallableTest(i));
sb.append(future.get()).append(";");
}
logger.info(sb.toString());
logger.info("都跑好了");
}
private class CallableTest implements Callable<String> {
int i;
public CallableTest(int i){
this.i = i;
}
@Override
public String call() throws Exception {
String s = i + "跑好了";
logger.info(s);
return s;
}
}
上述代码的执行结果为:
2021-05-24 11:55:31,299 [tttt-1] INFO c.y.s.b.c.controller.TestController [TestController.java : 45] - 0跑好了
2021-05-24 11:55:31,301 [tttt-2] INFO c.y.s.b.c.controller.TestController [TestController.java : 45] - 1跑好了
2021-05-24 11:55:31,302 [tttt-3] INFO c.y.s.b.c.controller.TestController [TestController.java : 45] - 2跑好了
2021-05-24 11:55:31,303 [tttt-4] INFO c.y.s.b.c.controller.TestController [TestController.java : 45] - 3跑好了
2021-05-24 11:55:31,304 [tttt-5] INFO c.y.s.b.c.controller.TestController [TestController.java : 45] - 4跑好了
2021-05-24 11:55:31,305 [http-nio-16100-exec-6] INFO c.y.s.b.c.controller.TestController [TestController.java : 30] - 0跑好了;1跑好了;2跑好了;3跑好了;4跑好了;
2021-05-24 11:55:31,306 [http-nio-16100-exec-6] INFO c.y.s.b.c.controller.TestController [TestController.java : 31] - 都跑好了