原文:http://blog.csdn.net/shimiso/article/details/8964527
spring配置
- <!-- 异步线程池 -->
- <bean id="taskExecutor"
- class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
- <!-- 核心线程数 -->
- <property name="corePoolSize" value="10" />
- <!-- 最大线程数 -->
- <property name="maxPoolSize" value="100" />
- <!-- 队列最大长度 >=mainExecutor.maxSize -->
- <property name="queueCapacity" value="1000" />
- <!-- 线程池维护线程所允许的空闲时间 -->
- <property name="keepAliveSeconds" value="300" />
- <!-- 线程池对拒绝任务(无线程可用)的处理策略 -->
- <property name="rejectedExecutionHandler">
- <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
- </property>
- </bean>
Java代码
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.task.TaskExecutor;
- public class ThreadPoolTest {
- @Autowired
- private TaskExecutor taskExecutor;// 线程池
- // 将创建的线程添加到线程池中
- public void test() throws Exception {
- for (int i = 0; i < 10; i++) {
- this.taskExecutor.execute(new AppContentDataPushThread());
- }
- }
- class AppContentDataPushThread implements Runnable {
- public AppContentDataPushThread() {
- }
- @Override
- public void run() {
- System.out.println("执行线程");
- }
- }
- }