1.前言
Springt通过任务执行器(TaskExecutor)来实现多线程和并发编程。使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor。而实际开发中任务一般是非阻碍的,即异步的,所以我们要在配置类中通过@EnableAsync 开启对异步任务的支持,并通过实际执行Bean的方法中使用@Async注解来声明其是一个异步任务。
2.基于springboot实现
1.配置类
配置类实现AsyncConfigurer接口,并重写getAsyncExecutor方法,并返回一个ThreadPoolTaskExecutor,这样我们就获得一个基于线程池TaskExecutor。
import java.util.concurrent.Executor;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
* @author caiqingheng
* @date 2020年8月7日
*
*/
@Configurable
@EnableAsync
public class CustomMultiThreadingConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor(){
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(10);
taskExecutor.setQueueCapacity(25);
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return AsyncConfigurer.super.getAsyncUncaughtExceptionHandler();
}
}
2.控制层
/**
* xcwlkj.com Inc.
* Copyright (c) 2015-2030 All Rights Reserved.
*/
package com.xcwlkj.fivedollars.controller.manager;
import javax.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xcwlkj.fivedollars.service.ArticleService;
import com.xcwlkj.util.wrapper.WrapMapper;
import com.xcwlkj.util.wrapper.Wrapper;
@RestController
@RequestMapping(value = "", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ArticleController extends BaseController {
@Resource
private ArticleService articleService;
@PostMapping(value = "/manager/article/queryArticleList")
public Wrapper<QueryArticleListRespModel> queryArticleList(@RequestBody QueryArticleListReqModel reqModel) {
//多线程异步调用测试
for (int i = 0; i < 10; i++) {
articleService.executeAsyncTask1(i);
articleService.executeAsyncTask2(i);
}
}
}
3.接口
import org.springframework.stereotype.Service;
import com.xcwlkj.fivedollars.model.dto.article.AddArticleDTO;
import com.xcwlkj.fivedollars.model.dto.article.DeleteArticleDTO;
import com.xcwlkj.fivedollars.model.dto.article.QueryArticleDetailDTO;
import com.xcwlkj.fivedollars.model.dto.article.QueryArticleListDTO;
import com.xcwlkj.fivedollars.model.dto.article.UpdateArticleDTO;
import com.xcwlkj.fivedollars.model.vo.article.QueryArticleDetailVO;
import com.xcwlkj.fivedollars.model.vo.article.QueryArticleListVO;
@Service
public interface ArticleService {
void executeAsyncTask1(int i);
void executeAsyncTask2(int i);
}
4.实现类
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service("articleService")
public class ArticleServiceImpl implements ArticleService {
@Async
@Override
public void executeAsyncTask1(int i) {
System.out.println("executeAsyncTask1======>{}"+i);
}
@Async
@Override
public void executeAsyncTask2(int i) {
System.out.println("executeAsyncTask2======>{}"+i);
}
}
5.访问
http://localhost:8662/manager/article/queryArticleList