日志正文
|
||
我们在应用中,常常会创建很多个线程,来处理任务.在java中,有一个现成的线程池可以选择,那就是ThreadPoolExecutor.
package com.taobao.danchen.threadpoolexecutor; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * @author danchen * */ public class TestThreadPoolExecutor { //最小线程数 private static final int minSize=10; //最大小线程数 private static final int maxSize=50; private static final int keepAliveTime=1; //缓冲队列大小 private static final int capacity=20; /** * @param args */ public static void main(String[] args) { //创建一个线程池 ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(minSize,maxSize,keepAliveTime,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(capacity)); //抛弃最新的 threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); //抛弃最老的 threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy()); //重试 threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //抛出异常 threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); } } 关于线程池使用过程中,出现的几种异常的处理策略 http://blog.csdn.net/robage/article/details/1620691
阅读(?)评论(0)
|
||
评论 想第一时间抢沙发么?