spring中的@EnableAsync注解@Async注解实现异步执⾏任务
spring中的@EnableAsync注解@Async注解实现异步执⾏任务
在平时使⽤多线程的时候⼤多是通过这⼏种⽅式:
1. 实现Thread接⼝
2. 继承Runable类
3. 使⽤线程池
但是在spring中提供了别的⽅式实现多线程功能,避免了⼤量冗余代码,就是使⽤@EnableAsync注解就可以了,将@EnableAsync注解加到创建线程池的配置类上(使⽤@Configuration注解修饰的类),将@Async注解加到需要异步执⾏的任务上(具体的实现⽅法)。
第⼀步定义配置类,配置线程池
fig;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import t.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.urrent.ThreadPoolTaskExecutor;
import urrent.Executor;
/**
* @Author: jeason
* @Description:
* @Date: 2020/11/7 19:17
*/
@Configuration
@EnableAsync
public class ThreadPoolConfigTest implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
int num = Runtime().availableProcessors();
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(num);
taskExecutor.setMaxPoolSize(num*5);
taskExecutor.setQueueCapacity(num*2);
taskExecutor.setThreadNamePrefix("taskExecutor->");
taskExecutor.initialize();
return taskExecutor;
}
/**
*异步任务中异常处理
*/
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SimpleAsyncUncaughtExceptionHandler();
}
}
第⼆步编写异步任务类
ample.springbootponent;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* @Author: jeason
* @Description:
* @Date: 2020/11/7 20:49
*/
@Component
public class TreadTasks {
@Async
public void startMyThreadTask(int i) throws InterruptedException {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName()+" : "+i);
System.out.println("startMyThreadTask: "+i);
}
}
第三步调⽤任务
ller;
ample.springbootponent.TreadTasks;
ample.springboot.service.ThreadService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;springboot aop
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import urrent.LinkedBlockingQueue;
import urrent.ThreadPoolExecutor;
import urrent.TimeUnit;
/**
* @author 26950
* @date 2020/11/7 12:09
*/
@Api("多线程练习")
@RestController
@RequestMapping("/thread")
public class ThreadController {
@Autowired
TreadTasks treadTasks;
@GetMapping("test3")
public void test3() throws InterruptedException {
System.out.println("test3获取主线程名称:"+Thread.currentThread().getName()); for(int i=0;i<10;i++) {
treadTasks.startMyThreadTask(i);
}
System.out.println("test3执⾏完成");
}
}
执⾏结果:
test3获取主线程名称:http-nio-9999-exec-5 test3执⾏完成
taskExecutor->2 : 0 startMyThreadTask: 0
taskExecutor->7 : 2 startMyThreadTask: 2
taskExecutor->3 : 3 startMyThreadTask: 3
taskExecutor->6 : 1 startMyThreadTask: 1
taskExecutor->1 : 7 startMyThreadTask: 7
taskExecutor->5 : 4 startMyThreadTask: 4
taskExecutor->4 : 5
taskExecutor->8 : 6 startMyThreadTask: 6 startMyThreadTask: 5
taskExecutor->2 : 8 startMyThreadTask: 8
taskExecutor->7 : 9 startMyThreadTask: 9
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论