ThreadPoolTaskExecutor
2021-06-25 本文已影响0人
Djbfifjd
一、线程池配置方式
ThreadPoolTaskExecutor 是 Spring 的线程池技术,利用它可以轻松的实现线程池。具体可参照 ThreadPoolExecutor。
1️⃣xml 配置方式
pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "/spring-beans.dtd">
<beans>
<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!--核心线程数 -->
<property name="corePoolSize" value="5"/>
<!-- 线程池维护线程所允许的空闲时间 -->
<property name="keepAliveSeconds" value="3000"/>
<!--最大线程数 -->
<property name="maxPoolSize" value="10"/>
<!-- 队列大小 -->
<property name="queueCapacity" value="1000"/>
<!-- 线程池对拒绝任务(无线程可用)的处理策略 -->
<property name="rejectedExecutionHandler">
<!-- AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常 -->
<!-- CallerRunsPolicy:主线程直接执行该任务,执行完之后尝试添加下一个任务到线程池中,
可以有效降低向线程池内添加任务的速度 -->
<!-- DiscardOldestPolicy:抛弃旧的任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
<!-- DiscardPolicy:抛弃当前任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
<bean class="java.util.concurrent.ThreadPoolExecutor$DiscardPolicy"/>
</property>
</bean>
</beans>
2️⃣配置类方式
@Configuration
public class ExecturConfig {
@Bean("taskExector")
public Executor taskExector() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
int i = Runtime.getRuntime().availableProcessors();//获取到服务器的cpu内核
executor.setCorePoolSize(5);//核心池大小
executor.setMaxPoolSize(100);//最大线程数
executor.setQueueCapacity(1000);//队列程度
executor.setKeepAliveSeconds(1000);//线程空闲时间
executor.setThreadNamePrefix("tsak-asyn");//线程前缀名称
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());//配置拒绝策略
return executor;
}
二、使用
@Resource(name="taskExecutor")
ThreadPoolTaskExecutor taskExecutor;
@AutoWired
ThreadPoolTaskExecutor taskExecutor
public class SpringThread extends Thread{
private int parameter;
public SpringThread(int parameter){
this.parameter = parameter;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ":执行了..." + parameter);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
测试:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.task.TaskExecutor;
public class App {
public static void main( String[] args ) {
ApplicationContext appContext =
new ClassPathXmlApplicationContext("file:applicationContext.xml");
TaskExecutor executor = (TaskExecutor) appContext.getBean("taskExecutor");
for (int i = 0; i < 10; i++) {
SpringThread t = new SpringThread(i);
executor.execute(t);
}
System.out.println("main process is finish .....");
}
}
结果:
taskExecutor-1:执行了...0
taskExecutor-2:执行了...1
taskExecutor-3:执行了...2
taskExecutor-4:执行了...3
taskExecutor-5:执行了...4
main process is finish .....
taskExecutor-1:执行了...5
taskExecutor-3:执行了...7
taskExecutor-4:执行了...8
taskExecutor-2:执行了...6
taskExecutor-5:执行了...9