关于不允许使用 Executors 去创建线程池,而改用ThreadPoolExecutor去创建的解决办法

原代码:

ExecutorService executor = Executors.newFixedThreadPool(runSize);

修改后代码:

BlockingQueue workQueue=new ArrayBlockingQueue(100);
ThreadFactory threadFactory = Executors.defaultThreadFactory();
RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.AbortPolicy();
ExecutorService executor = new ThreadPoolExecutor(runSize, runSize + 1, 3000, TimeUnit.MILLISECONDS,workQueue,threadFactory,rejectedExecutionHandler);

ThreadPoolExecutor的方法如下

public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler);

你可能感兴趣的:(关于不允许使用 Executors 去创建线程池,而改用ThreadPoolExecutor去创建的解决办法)