当前位置: 移动技术网 > IT编程>开发语言>Java > Spring线程池ThreadPoolTaskExecutor配置详情

Spring线程池ThreadPoolTaskExecutor配置详情

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文介绍了spring线程池threadpooltaskexecutor配置,分享给大家,具体如下:

1. threadpooltaskexecutor配置

<!-- 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

@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

/**
 * 多线程并发处理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

@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

 @configuration
 @componentscan(basepackages = { "com.xxx.multithread" })
 @importresource(value = { "classpath:config/application-task.xml" })
 @enablescheduling
 public class multithreadconfig {
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网