spring整合线程池
搭建spring或者springboot环境;
1.1 配置线程池
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package com.swagger.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.ThreadPoolExecutor;
@Configuration @EnableAsync public class ExecutorConfig {
@Bean("asyncServiceExecutor") public ThreadPoolTaskExecutor asyncServiceExecutor(){ ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(5); executor.setMaxPoolSize(5); executor.setQueueCapacity(99999); executor.setThreadNamePrefix("async-service-"); executor.setWaitForTasksToCompleteOnShutdown(true); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.initialize(); return executor; }
}
|
1.2 定义测试业务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @Component public class TaskServiceA {
@Async("asyncServiceExecutor") public void add() throws InterruptedException { TimeUnit.SECONDS.sleep(1); System.out.println("add run...."); }
@Async("asyncServiceExecutor") public void update() throws InterruptedException { TimeUnit.SECONDS.sleep(1); System.out.println("update run...."); }
@Async("asyncServiceExecutor") public void delete() throws InterruptedException { TimeUnit.SECONDS.sleep(1); System.out.println("delete run...."); }
}
|
同步执行add(),update(),delete()时,执行时间大致为3s;
@Async注解表示每次调用开启一个新的线程异步执行,执行时间大大缩短;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Component public class TaskServiceB {
public String add() throws InterruptedException { TimeUnit.SECONDS.sleep(1); return "add"; }
public String update() throws InterruptedException { TimeUnit.SECONDS.sleep(1); return "update"; }
public String delete() throws InterruptedException { TimeUnit.SECONDS.sleep(1); return "delete"; }
}
|
测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| @RestController public class TaskController {
@Autowired private TaskServiceA taskServiceA;
@Autowired private TaskServiceB taskServiceB;
@Autowired private ThreadPoolTaskExecutor asyncServiceExecutor;
@GetMapping("/task0") public String asyncTask0() throws InterruptedException { long start = System.currentTimeMillis(); taskServiceA.add(); taskServiceA.delete(); taskServiceA.update(); long end = System.currentTimeMillis(); System.out.println((end-start)/1000); return "success"; }
@GetMapping("/task1") public String syncTask() throws InterruptedException { long start = System.currentTimeMillis(); String add = taskServiceB.add(); String delete = taskServiceB.delete(); String update = taskServiceB.update(); long end = System.currentTimeMillis(); System.out.println((end-start)/1000); return add+"||"+delete+"||"+update; }
@GetMapping("/task2") public String asyncTask() throws InterruptedException, ExecutionException { long start = System.currentTimeMillis(); Future<String> futureAdd = asyncServiceExecutor.submit(() -> { String result = null; try { result= taskServiceB.add(); } catch (InterruptedException e) { e.printStackTrace(); } return result; });
Future<String> futureDelete = asyncServiceExecutor.submit(() -> { String result = null; try { result= taskServiceB.delete(); } catch (InterruptedException e) { e.printStackTrace(); } return result; });
Future<String> futureUpdate = asyncServiceExecutor.submit(() -> { String result = null; try { result= taskServiceB.update(); } catch (InterruptedException e) { e.printStackTrace(); } return result; }); String add = futureAdd.get(); String delete = futureDelete.get(); String update = futureUpdate.get(); long end = System.currentTimeMillis(); System.out.println((end-start)/1000); return add+"||"+delete+"||"+update; } }
|