原文: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("執行線程");
}
}
}