本文介绍了Spring线程池ThreadPoolTaskExecutor配置,分享给大家,具体如下:
1. ThreadPoolTaskExecutor配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- spring thread pool executor --> < bean id = "taskExecutor" class = "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" > <!-- 线程池维护线程的最少数量 --> < property name = "corePoolSize" value = "5" /> <!-- 允许的空闲时间 --> < property name = "keepAliveSeconds" value = "200" /> <!-- 线程池维护线程的最大数量 --> < property name = "maxPoolSize" value = "10" /> <!-- 缓存队列 --> < property name = "queueCapacity" value = "20" /> <!-- 对拒绝task的处理策略 --> < property name = "rejectedExecutionHandler" > < bean class = "java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" /> </ property > </ bean > |
属性字段说明
corePoolSize:线程池维护线程的最少数量
keepAliveSeconds:允许的空闲时间
maxPoolSize:线程池维护线程的最大数量
queueCapacity:缓存队列
rejectedExecutionHandler:对拒绝task的处理策略
2. execute(Runable)方法执行过程
如果此时线程池中的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程来处理被添加的任务。
如果此时线程池中的数量等于 corePoolSize,但是缓冲队列 workQueue未满,那么任务被放入缓冲队列。
如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量小于maxPoolSize,建新的线程来处理被添加的任务。
如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量等于maxPoolSize,那么通过handler所指定的策略来处理此任务。也就是:处理任务的优先级为:核心线程corePoolSize、任务队列workQueue、最大线程 maximumPoolSize,如果三者都满了,使用handler处理被拒绝的任务。
当线程池中的线程数量大于corePoolSize时,如果某线程空闲时间超过keepAliveTime,线程将被终止。这样,线程池可以动态的调整池中的线程数。
3. 示例代码
Junit Test
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
|
@RunWith (SpringJUnit4ClassRunner. class ) @ContextConfiguration (classes = { MultiThreadConfig. class }) public class MultiThreadTest { @Autowired private ThreadPoolTaskExecutor taskExecutor; @Autowired private MultiThreadProcessService multiThreadProcessService; @Test public void test() { int n = 20 ; for ( int i = 0 ; i < n; i++) { taskExecutor.execute( new MultiThreadDemo(multiThreadProcessService)); System.out.println( "int i is " + i + ", now threadpool active threads totalnum is " + taskExecutor.getActiveCount()); } try { System.in.read(); } catch (IOException e) { throw new RuntimeException(e); } } } |
MultiThreadDemo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/** * 多线程并发处理demo * @author daniel.zhao * */ public class MultiThreadDemo implements Runnable { private MultiThreadProcessService multiThreadProcessService; public MultiThreadDemo() { } public MultiThreadDemo(MultiThreadProcessService multiThreadProcessService) { this .multiThreadProcessService = multiThreadProcessService; } @Override public void run() { multiThreadProcessService.processSomething(); } } |
MultiThreadProcessService
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@Service public class MultiThreadProcessService { public static final Logger logger = Logger.getLogger(MultiThreadProcessService. class ); /** * 默认处理流程耗时1000ms */ public void processSomething() { logger.debug( "MultiThreadProcessService-processSomething" + Thread.currentThread() + "......start" ); try { Thread.sleep( 1000 ); } catch (InterruptedException e) { throw new RuntimeException(e); } logger.debug( "MultiThreadProcessService-processSomething" + Thread.currentThread() + "......end" ); } } |
MultiThreadConfig
1
2
3
4
5
6
|
@Configuration @ComponentScan (basePackages = { "com.xxx.multithread" }) @ImportResource (value = { "classpath:config/application-task.xml" }) @EnableScheduling public class MultiThreadConfig { } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/redcool/p/6426173.html