1 Maven依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--hutool工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.1</version>
</dependency>
2 Future.isDone()监控子线程的执行结果
2.1 实现思路
子线程会返回一个Future对象,通过Future.isDone()判断子线程是否结束。
2.2 子线程
package com.service;
import cn.hutool.core.date.DateUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;
@Service
public class FutureAsyncService {
/**
* 测试Future子线程
*
* @return
*/
@Async
public Future futureSubThread(int i) throws Exception {
Thread.sleep(100);
System.out.print(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + " " + i + " ");
return new AsyncResult(i);
}
}
2.3 调试代码
package com.service;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;
@Service
public class AsyncService {
@Autowired
private FutureAsyncService futureAsyncService;
/**
* 测试Future监控子线程的执行结果
*/
public void testFuture() throws Exception {
List<Future> futureList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Future future = futureAsyncService.futureSubThread(i);
futureList.add(future);
}
System.out.print(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + ":退出循环\t");
while (true) {
if (CollectionUtil.isNotEmpty(futureList)) {
boolean isAllDone = true;
for (Future future : futureList) {
if (future == null || future.isDone() == false) {
isAllDone = false;
}
}
if (isAllDone == true) {
System.out.print(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + ":执行完毕\t");
break;
}
}
}
}
}
2.4 调试结果
2021-09-17 10:51:31.053:退出循环 2021-09-17 10:51:31.156 6 2021-09-17 10:51:31.156 3
2021-09-17 10:51:31.156 0 2021-09-17 10:51:31.156 7 2021-09-17 10:51:31.156 4
2021-09-17 10:51:31.156 5 2021-09-17 10:51:31.156 2 2021-09-17 10:51:31.156 1
2021-09-17 10:51:31.256 15 2021-09-17 10:51:31.256 13 2021-09-17 10:51:31.256 12
2021-09-17 10:51:31.256 11 2021-09-17 10:51:31.256 8 2021-09-17 10:51:31.256 14
2021-09-17 10:51:31.256 10 2021-09-17 10:51:31.256 9 2021-09-17 10:51:31.357 17
2021-09-17 10:51:31.357 23 2021-09-17 10:51:31.357 20 2021-09-17 10:51:31.357 19
2021-09-17 10:51:31.357 22 2021-09-17 10:51:31.357 21 2021-09-17 10:51:31.357 18
2021-09-17 10:51:31.357 16 2021-09-17 10:51:31.458 30 2021-09-17 10:51:31.458 25
2021-09-17 10:51:31.458 26 2021-09-17 10:51:31.458 24 2021-09-17 10:51:31.458 27
2021-09-17 10:51:31.458 31 2021-09-17 10:51:31.458 29 2021-09-17 10:51:31.458 28
2021-09-17 10:51:31.559 34 2021-09-17 10:51:31.559 36 2021-09-17 10:51:31.559 32
2021-09-17 10:51:31.559 39 2021-09-17 10:51:31.559 38 2021-09-17 10:51:31.559 33
2021-09-17 10:51:31.559 35 2021-09-17 10:51:31.559 37 2021-09-17 10:51:31.659 42
2021-09-17 10:51:31.659 44 2021-09-17 10:51:31.659 40 2021-09-17 10:51:31.659 43
2021-09-17 10:51:31.659 45 2021-09-17 10:51:31.659 41 2021-09-17 10:51:31.659 46
2021-09-17 10:51:31.659 47 2021-09-17 10:51:31.760 52 2021-09-17 10:51:31.760 55
2021-09-17 10:51:31.760 50 2021-09-17 10:51:31.760 54 2021-09-17 10:51:31.760 51
2021-09-17 10:51:31.760 53 2021-09-17 10:51:31.760 49 2021-09-17 10:51:31.760 48
2021-09-17 10:51:31.861 59 2021-09-17 10:51:31.861 58 2021-09-17 10:51:31.861 61
2021-09-17 10:51:31.861 56 2021-09-17 10:51:31.861 60 2021-09-17 10:51:31.861 57
2021-09-17 10:51:31.861 63 2021-09-17 10:51:31.861 62 2021-09-17 10:51:31.962 70
2021-09-17 10:51:31.962 68 2021-09-17 10:51:31.962 66 2021-09-17 10:51:31.962 64
2021-09-17 10:51:31.962 71 2021-09-17 10:51:31.962 69 2021-09-17 10:51:31.962 67
2021-09-17 10:51:31.962 65 2021-09-17 10:51:32.062 73 2021-09-17 10:51:32.062 76
2021-09-17 10:51:32.062 75 2021-09-17 10:51:32.062 78 2021-09-17 10:51:32.062 74
2021-09-17 10:51:32.062 72 2021-09-17 10:51:32.062 79 2021-09-17 10:51:32.062 77
2021-09-17 10:51:32.163 85 2021-09-17 10:51:32.163 80 2021-09-17 10:51:32.163 82
2021-09-17 10:51:32.163 86 2021-09-17 10:51:32.163 84 2021-09-17 10:51:32.163 87
2021-09-17 10:51:32.163 81 2021-09-17 10:51:32.163 83 2021-09-17 10:51:32.264 91
2021-09-17 10:51:32.264 90 2021-09-17 10:51:32.264 89 2021-09-17 10:51:32.264 92
2021-09-17 10:51:32.264 88 2021-09-17 10:51:32.264 94 2021-09-17 10:51:32.264 95
2021-09-17 10:51:32.264 93 2021-09-17 10:51:32.364 97 2021-09-17 10:51:32.364 98
2021-09-17 10:51:32.364 99 2021-09-17 10:51:32.364 96 2021-09-17 10:51:32.364:执行完毕
3 事件监听器监控子线程的执行结果
3.1 实现思路
子线程结束就会发布一个事件,通过监听器监听事件判断子线程是否全部结束。
3.2 定义事件
package com.event;
import org.springframework.context.ApplicationEvent;
/**
* 异步事件
*/
public class AsyncEvent extends ApplicationEvent {
public AsyncEvent(Object source) {
super(source);
}
}
3.3 发布事件
package com.service;
import cn.hutool.core.date.DateUtil;
import com.event.AsyncEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
@Service
public class EventAsyncService {
/**
* 注入ApplicationEventPublisher用来发布事件
*/
@Resource
private ApplicationEventPublisher publisher;
/**
* 测试Event子线程
*
* @return
*/
@Async
public void eventSubThread(int i) throws Exception {
Thread.sleep(100);
System.out.print(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + " " + i + " ");
//发布事件
publisher.publishEvent(new AsyncEvent(this));
}
}
3.4 事件监听器
package com.service;
import cn.hutool.core.date.DateUtil;
import com.event.AsyncEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
@Service
public class EventAsyncService {
/**
* 注入ApplicationEventPublisher用来发布事件
*/
@Resource
private ApplicationEventPublisher publisher;
/**
* 测试Event子线程
*
* @return
*/
@Async
public void eventSubThread(int i) throws Exception {
Thread.sleep(100);
System.out.print(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + " " + i + " ");
//发布事件
publisher.publishEvent(new AsyncEvent(this));
}
}
3.5 调试代码
package com.service;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class AsyncService {
@Autowired
private EventAsyncService eventAsyncService;
/**
* 测试事件监听器监控子线程的执行结果
*/
public void testEvent() throws Exception {
for (int i = 0; i < 100; i++) {
eventAsyncService.eventSubThread(i);
}
System.out.print(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + ":退出循环\t");
}
}
3.6 调试结果
2021-09-17 13:34:38.572:退出循环 2021-09-17 13:34:38.675 0 2021-09-17 13:34:38.675 5
2021-09-17 13:34:38.675 1 2021-09-17 13:34:38.675 4 2021-09-17 13:34:38.675 2
2021-09-17 13:34:38.675 3 2021-09-17 13:34:38.675 7 2021-09-17 13:34:38.675 6
2021-09-17 13:34:38.782 10 2021-09-17 13:34:38.782 12 2021-09-17 13:34:38.782 11
2021-09-17 13:34:38.782 13 2021-09-17 13:34:38.782 9 2021-09-17 13:34:38.782 14
2021-09-17 13:34:38.782 15 2021-09-17 13:34:38.782 8 2021-09-17 13:34:38.883 19
2021-09-17 13:34:38.883 16 2021-09-17 13:34:38.883 18 2021-09-17 13:34:38.883 21
2021-09-17 13:34:38.883 20 2021-09-17 13:34:38.883 17 2021-09-17 13:34:38.883 23
2021-09-17 13:34:38.883 22 2021-09-17 13:34:38.985 24 2021-09-17 13:34:38.986 30
2021-09-17 13:34:38.986 27 2021-09-17 13:34:38.986 26 2021-09-17 13:34:38.986 29
2021-09-17 13:34:38.986 28 2021-09-17 13:34:38.986 25 2021-09-17 13:34:38.987 31
2021-09-17 13:34:39.085 32 2021-09-17 13:34:39.088 36 2021-09-17 13:34:39.088 38
2021-09-17 13:34:39.088 37 2021-09-17 13:34:39.088 35 2021-09-17 13:34:39.088 33
2021-09-17 13:34:39.088 34 2021-09-17 13:34:39.089 39 2021-09-17 13:34:39.185 40
2021-09-17 13:34:39.189 43 2021-09-17 13:34:39.189 45 2021-09-17 13:34:39.189 41
2021-09-17 13:34:39.189 46 2021-09-17 13:34:39.189 42 2021-09-17 13:34:39.189 44
2021-09-17 13:34:39.190 47 2021-09-17 13:34:39.285 48 2021-09-17 13:34:39.289 53
2021-09-17 13:34:39.289 51 2021-09-17 13:34:39.289 52 2021-09-17 13:34:39.289 54
2021-09-17 13:34:39.289 49 2021-09-17 13:34:39.289 50 2021-09-17 13:34:39.290 55
2021-09-17 13:34:39.386 56 2021-09-17 13:34:39.390 60 2021-09-17 13:34:39.390 59
2021-09-17 13:34:39.390 57 2021-09-17 13:34:39.390 62 2021-09-17 13:34:39.390 61
2021-09-17 13:34:39.390 58 2021-09-17 13:34:39.391 63 2021-09-17 13:34:39.486 64
2021-09-17 13:34:39.490 65 2021-09-17 13:34:39.490 70 2021-09-17 13:34:39.490 66
2021-09-17 13:34:39.490 69 2021-09-17 13:34:39.490 67 2021-09-17 13:34:39.490 68
2021-09-17 13:34:39.491 71 2021-09-17 13:34:39.587 72 2021-09-17 13:34:39.591 73
2021-09-17 13:34:39.591 76 2021-09-17 13:34:39.591 78 2021-09-17 13:34:39.591 77
2021-09-17 13:34:39.591 75 2021-09-17 13:34:39.591 74 2021-09-17 13:34:39.592 79
2021-09-17 13:34:39.687 80 2021-09-17 13:34:39.691 84 2021-09-17 13:34:39.691 82
2021-09-17 13:34:39.691 81 2021-09-17 13:34:39.691 86 2021-09-17 13:34:39.691 85
2021-09-17 13:34:39.691 83 2021-09-17 13:34:39.692 87 2021-09-17 13:34:39.788 88
2021-09-17 13:34:39.792 90 2021-09-17 13:34:39.792 92 2021-09-17 13:34:39.792 94
2021-09-17 13:34:39.792 91 2021-09-17 13:34:39.792 93 2021-09-17 13:34:39.792 89
2021-09-17 13:34:39.793 95 2021-09-17 13:34:39.888 96 2021-09-17 13:34:39.892 98
2021-09-17 13:34:39.892 99 2021-09-17 13:34:39.892 97 2021-09-17 13:34:39.892:执行完毕
注:
(1)有关多线程的配置请查看以下博客。
Spring Boot配置多线程(创建、使用)https://blog.csdn.net/qq_38974638/article/details/120145228
(2) 有关调用异步方法创建线程的方式请查看以下博客。