Spring 线程池应用
线程池Demo之 ThreadPoolTaskExecutor
(1)编写测试类
Java代码
import org.springframework.core.task.TaskExecutor;
public class TaskExecutorExample {
private TaskExecutor taskExecutor;
public TaskExecutorExample(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void printMessages() {
for(int i = 0; i < 25; i++) {
taskExecutor.execute(new MessagePrinterTask("Message" + i));
}
}
private class MessagePrinterTask implements Runnable {
private String message;
public MessagePrinterTask(String message) {
this.message = message;
}
public void run() {
System.out.println(message);
}
}
}
<strong>(2)spring的配置 </strong>Xml代码 <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="5" /> <property name="maxPoolSize" value="10" /> <property name="queueCapacity" value="25" /> </bean> <bean id="taskExecutorExample" class="powercn.TaskExecutorExample"> <constructor-arg ref="taskExecutor" /> </bean>
<span style="font-family:Georgia;font-size:12px;line-height: 22px;">线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) corePoolSize: 线程池维护线程的最少数量 maximumPoolSize:线程池维护线程的最大数量 keepAliveTime: 线程池维护线程所允许的空闲时间 unit: 线程池维护线程所允许的空闲时间的单位 workQueue: 线程池所使用的缓冲队列 handler: 线程池对拒绝任务的处理策略 一个任务通过 execute(Runnable)方法被添加到线程池,任务就是一个 Runnable类型的对象,任务的执行方法就是 Runnable类型对象的run()方法。 当一个任务通过execute(Runnable)方法欲添加到线程池时: 如果此时线程池中的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程来处理被添加的任务。 如果此时线程池中的数量等于 corePoolSize,但是缓冲队列 workQueue未满,那么任务被放入缓冲队列。 如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量小于maximumPoolSize,建新的线程来处理被添加的任务。 如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量等于maximumPoolSize,那么通过 handler所指定的策略来处理此任务。 也就是:处理任务的优先级为: 核心线程corePoolSize、任务队列workQueue、最大线程maximumPoolSize,如果三者都满了,使用handler处理被拒绝的任务。 当线程池中的线程数量大于 corePoolSize时,如果某线程空闲时间超过keepAliveTime,线程将被终止。这样,线程池可以动态的调整池中的线程数。 unit可选的参数为java.util.concurrent.TimeUnit中的几个静态属性: NANOSECONDS、MICROSECONDS、MILLISECONDS、SECONDS。 workQueue我常用的是:java.util.concurrent.ArrayBlockingQueue handler有四个选择: ThreadPoolExecutor.AbortPolicy() 抛出java.util.concurrent.RejectedExecutionException异常 ThreadPoolExecutor.CallerRunsPolicy() 重试添加当前的任务,他会自动重复调用execute()方法 ThreadPoolExecutor.DiscardOldestPolicy() 抛弃旧的任务 ThreadPoolExecutor.DiscardPolicy() 抛弃当前的任务</span>
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" scope="singleton" lazy-init="true"> <property name="corePoolSize" value="${threadpool.corePoolSize}" /> <property name="queueCapacity" value="${threadpool.queueCapacity}" /> <property name="maxPoolSize" value="${threadpool.maxPoolSize}" /> <property name="keepAliveSeconds" value="${threadpool.keepAliveSeconds}" /> <property name="rejectedExecutionHandler"> <bean class="${threadpool.rejectedExecutionHandler}" /> </property>
<!--作用是修改线程的名字--> <property name="threadNamePrefix" value="${threadpool.threadNamePrefix}" /> </bean>
<strong>properties文件:</strong>
threadpool.corePoolSize=5 threadpool.queueCapacity=30 threadpool.maxPoolSize=20 threadpool.keepAliveSeconds=5 threadpool.rejectedExecutionHandler=java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy threadpool.threadNamePrefix=myThread <strong> (3)调用 </strong>Java代码
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml"); TaskExecutorExample te = (TaskExecutorExample)appContext.getBean("taskExecutorExample"); te.printMessages(); System.out.println("11111111111111111111111"); <span style="color:#3366ff;"><strong> <span style="color:#000000;"> (4)效果 </span></strong></span><img alt="" src="http://dl.iteye.com/upload/attachment/316620/37c3fd12-508c-3e7a-9e0b-64a2c449c8c2.gif" style="border: 0px; max-width: 100%;" />