@RequestMapping("/api/async/v1")
public class AsyncController {
private AsynService asynService;
public Object findDetailById() throws InterruptedException, ExecutionException {
long currentTimeMillis = System.currentTimeMillis();
long currentTimeMillis1 = System.currentTimeMillis();
String result = "task任务总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms";
public interface AsynService {
void asyncTest1() throws ExecutionException, InterruptedException;
public class AsynServiceImpl implements AsynService {
private AsyncTask asyncTask;
AddressMapper addressMapper;
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void asyncTest() {
} catch (InterruptedException e) {
} catch (InterruptedException e) {
} catch (InterruptedException e) {
if(task1.isDone() && task2.isDone() && task3.isDone()) {
System.out.println("最后执行了");
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void asyncTest1() throws ExecutionException, InterruptedException {
CompletableFuture future1=CompletableFuture.supplyAsync(new Supplier() {
UserDO userDO = new UserDO();
userDO.setMail("37396916374@qq.com");
userMapper.insert(userDO);
} catch (InterruptedException e) {
CompletableFuture nestedResult = future1.thenCompose(value->
CompletableFuture.supplyAsync(()->{
AddressDO addressDO = new AddressDO();
addressDO.setUserId(value);
addressDO.setPhone("18710555348");
addressMapper.insert(addressDO);
} catch (InterruptedException e) {
return addressDO.getId();
if(future1.isDone()&& nestedResult.isDone()){
System.out.println(nestedResult.get());
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void asyncTest3() {
CompletableFuture future1=CompletableFuture.supplyAsync(new Supplier() {
UserDO userDO = new UserDO();
userDO.setMail("33296912374@qq.com");
userMapper.insert(userDO);
CompletableFuture nestedResult = future1.thenCompose(value->
CompletableFuture.supplyAsync(()->{
AddressDO addressDO = new AddressDO();
addressDO.setUserId(value);
addressDO.setPhone("18710555348");
addressMapper.insert(addressDO);
return addressDO.getId();
if(future1.isDone()&& nestedResult.isDone()){

AddressMapper addressMapper;
public Future
t1() throws InterruptedException { public Future t2() throws InterruptedException {
public Future t3() throws InterruptedException {
@Async("asyncServiceExecutor")
public Future
task1() throws InterruptedException{ long currentTimeMillis = System.currentTimeMillis();
AddressDO addressDO = new AddressDO();
addressDO.setPhone("18710555348");
addressMapper.insert(addressDO);
long currentTimeMillis1 = System.currentTimeMillis();
System.out.println("task1任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
return new AsyncResult(addressDO);
@Async("asyncServiceExecutor")
public Future task2() throws InterruptedException{
long currentTimeMillis = System.currentTimeMillis();
UserDO userDO = new UserDO();
userDO.setMail("3499283774@qq.com");
userMapper.insert(userDO);
long currentTimeMillis1 = System.currentTimeMillis();
System.out.println("task2任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
return new AsyncResult(userDO);
public void test7() throws InterruptedException {
FixedThreadPoolUtil.doExecutor(() -> {
} catch (InterruptedException e) {
FixedThreadPoolUtil.doExecutor(() -> {
} catch (InterruptedException e) {
public void task4() throws InterruptedException {
long currentTimeMillis = System.currentTimeMillis();
AddressDO addressDO = new AddressDO();
addressDO.setCity("ti京");
addressDO.setPhone("18710555348");
addressMapper.insert(addressDO);
long currentTimeMillis1 = System.currentTimeMillis();
System.out.println("task4任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
public void task5() throws InterruptedException {
long currentTimeMillis = System.currentTimeMillis();
AddressDO addressDO = new AddressDO();
addressDO.setPhone("18710555348");
addressMapper.insert(addressDO);
long currentTimeMillis1 = System.currentTimeMillis();
System.out.println("task5任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
@Async("asyncServiceExecutor")
public Future task3() throws InterruptedException{
long currentTimeMillis = System.currentTimeMillis();
long currentTimeMillis1 = System.currentTimeMillis();
System.out.println("task3任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
return new AsyncResult("");

public class ThreadPoolConfiguration {
@Bean(name = "defaultThreadPoolExecutor", destroyMethod = "shutdown")
public ThreadPoolExecutor systemCheckPoolExecutorService(){
return new ThreadPoolExecutor(3,10,60, TimeUnit.SECONDS,
new LinkedBlockingDeque<>(10000),
Executors.defaultThreadFactory(),
(r,e)->System.out.println("is full"));
public class ExecutorConfig {
@Value("${async.executor.thread.core_pool_size}")
private int corePoolSize;
@Value("${async.executor.thread.max_pool_size}")
@Value("${async.executor.thread.queue_capacity}")
private int queueCapacity;
@Value("${async.executor.thread.name.prefix}")
private String namePrefix;
@Bean(name = "asyncServiceExecutor")
public Executor asyncServiceExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setThreadNamePrefix(namePrefix);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
async.executor.thread.core_pool_size=4
async.executor.thread.max_pool_size=8
async.executor.thread.queue_capacity=135
async.executor.thread.name.prefix= ticketManager-worker-