1.定义异步任务
(1)代码示例如下:
@Component
@Async
public class AsyncTask {
public void task1() throws InterruptedException {
long start = System.currentTimeMillis();
Thread.sleep(3000);
long end = System.currentTimeMillis();
System.out.println("当前线程:" + Thread.currentThread().getName() + "," + "任务一耗时:" + (end - start) + "ms");}
public void task2() throws InterruptedException {
long start = System.currentTimeMillis();
Thread.sleep(3000);
long end = System.currentTimeMillis();
System.out.println("当前线程:" + Thread.currentThread().getName() + "," + "任务二耗时:" + (end - start) + "ms");}
public void task3() throws InterruptedException {
long start = System.currentTimeMillis();
Thread.sleep(3000);
long end = System.currentTimeMillis();
System.out.println("当前线程:" + Thread.currentThread().getName() + "," + "任务三耗时:" + (end - start) + "ms");}
(2)@Async表明是一个异步任务,可以加在方法和类上
2.调用异步任务
@RestController
public class TaskService {
@Autowired
private AsyncTask asyncTask;
@RequestMapping("/taskService")
public String doTask() throws InterruptedException {
long start = System.currentTimeMillis();
asyncTask.task1();
asyncTask.task2();
asyncTask.task3();
long end = System.currentTimeMillis();
System.out.println("任务总耗时:" + (end - start) + "ms");
return "任务总耗时:" + (end - start) + "ms";}}
3.开启异步任务
(1)代码示例如下:
@EnableAsync
@SpringBootApplication
public class ScheduleTaskApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduleTaskApplication.class, args);}
(2)SpringBoot异步任务默认使用的线程池为SimpleAsyncTaskExecutor,特点如下:
<1>默认定义多少异步任务,创建多少线程
1.1.创建线程数量太多,占用内存过大,会造成OutOfMemoryError
<2>SimpleAsyncTaskExecutor不提供拒绝策略机制
<3>SimpleAsyncTaskExecutor可通过设置参数concurrencyLimit
3.1.指定启用的线程数目
3.2.默认concurrencyLimit取值为-1,即不启用资源节流
4.如果不使用默认的线程池可以自定义异步任务线程池
(1)自定义异步任务线程池有两种方式
(2)第一种:启动类继承AsyncConfigurerSupport
<1>代码示例如下:
@SpringBootApplication
@EnableAsync
public class Application extends AsyncConfigurerSupport {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);}
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("GithubLookup-");
executor.initialize();
return executor;}
(3)第二种:自定义配置类实现AsyncConfigurer
<1>代码示例如下:
@Configuration
public class AsyncThreadPoolConfig implements AsyncConfigurer {
/**
* 自定义线程池
* @return 线程池对象
*/
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(5);
threadPoolTaskExecutor.setMaxPoolSize(8);
threadPoolTaskExecutor.setQueueCapacity(10);
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;}
/**
* 自定义异常处理器
* @return 异常处理器对象
*/
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
AsyncUncaughtExceptionHandler syncUncaughtExceptionHandler = (ex, method, params) -> ex.printStackTrace();
return syncUncaughtExceptionHandler;}}}
5.获取异步任务对应的线程名称
(1)线程池从默认的SimpleAsyncTaskExecutor转换成自定义的ThreadPoolTaskExecutor