spring动态线程池(实质还是用了java的线程池) @Async
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 异步线程池 --> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!-- 核心线程数 --> <property name="corePoolSize" value="8" /> <!-- 最大线程数 --> <property name="maxPoolSize" value="100" /> <!-- 队列最大长度 >=mainExecutor.maxSize --> <property name="queueCapacity" value="0" /> <!-- 线程池维护线程所允许的空闲时间 --> <property name="keepAliveSeconds" value="500" /> <!-- 线程池对拒绝任务(无线程可用)的处理策略 --> <property name="rejectedExecutionHandler"> <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" /> </property> </bean> </beans>
public class MyCallable implements Callable<String> { private String name; public MyCallable(String name){ this.name=name; } @Override public String call() throws Exception { Thread.sleep(5000); return name; } }
import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorCompletionService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; public class threadPoolTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor) ctx.getBean("taskExecutor"); CompletionService<String> cs = new ExecutorCompletionService<>(taskExecutor); for (int i = 0; i < 5; i++) { cs.submit(new MyCallable("Thread "+i)); } for (int i = 0; i < 5; i++){ try { System.out.println(cs.take().get()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext1.xml"); ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor) ctx.getBean("taskExecutor"); List<Future<String>> list = new ArrayList<Future<String>>(); for (int i = 0; i < 5; i++) { Future<String> future= taskExecutor.submit(new MyCallable("Thread "+i)); list.add(future); } for (Iterator iterator = list.iterator(); iterator.hasNext();) { Future<String> future = (Future<String>) iterator.next(); try { System.out.println(future.get()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } iterator.remove(); } System.out.println(list.size()); }
queueCapacity参数是0的话,用的是SynchronousQueue,否则是LinkedBlockingQueue。
用CallerRunsPolicy策略的话,线程超过最大数量和队列长度之和,主线程等待,直到线程池有空闲线程。
------------
application.yml
spring:
application.name: TempestMongo
task.execution:
thread-name-prefix: SysAsyncExecutor_
pool:
core-size: 10
queue-capacity: 50
max-size: 20
keep-alive: 10s
allow-core-thread-timeout: false
logging:
config: config/log4j2.xml
@Resource
ThreadPoolTaskExecutor applicationTaskExecutor;
package com.example; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.ThreadPoolExecutor; @Configuration public class ThreadPoolConfiguration { @Bean public ThreadPoolTaskExecutor mythreadpool(){ ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); //设置核心线程数 taskExecutor.setCorePoolSize(8); //设置最大线程数 taskExecutor.setMaxPoolSize(16); //设置线程空闲等待时间 taskExecutor.setKeepAliveSeconds(60); //设置任务等待队列的大小 taskExecutor.setQueueCapacity(100); taskExecutor.setAllowCoreThreadTimeOut(false); // 设置线程池内线程名称的前缀-------阿里编码规约推荐--方便出错后进行调试 taskExecutor.setThreadNamePrefix("mythreadpool-"); //设置任务的拒绝策略 taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //初始化 taskExecutor.initialize(); return taskExecutor; } }
【springboot】@Async线程默认配置
http://t.zoukankan.com/kiko2014551511-p-12754927.html
Spring之——两种任务调度Scheduled和Async
https://blog.csdn.net/l1028386804/article/details/72494169/
springboot项目中线程池的使用
https://www.vvzixun.com/code/33dec0734daf29abefae90e86e5c9b46
SpringBoot项目@Async方法问题解决方案
https://www.jb51.net/article/185000.htm
springboot使用@async实现异步线程池
https://zhuanlan.zhihu.com/p/338100209
SpringBoot的线程调度
https://www.cnblogs.com/lasdaybg/p/10161249.html
@Configuration public class ThreadPoolConfiguration { private static final String THREAD_PREFFIX = "myThreadPool-"; @Value("${question.num:10}") private int questionNum; @Bean public ThreadPoolExecutor myThreadPool() { ThreadFactory namedThreadFactory = new ThreadFactory() { private final AtomicInteger threadNumber = new AtomicInteger(1); @Override public Thread newThread(Runnable r) { return new Thread(r, THREAD_PREFFIX + threadNumber.getAndIncrement()); } }; return new ThreadPoolExecutor(questionNum, questionNum, NumericConstants.ZERO, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), namedThreadFactory, new ThreadPoolExecutor.CallerRunsPolicy()); } }