当前位置: 代码迷 >> 综合 >> 关于不允许使用 Executors 去创建线程池,而改用ThreadPoolExecutor去创建的解决办法
  详细解决方案

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

热度:16   发布时间:2023-12-18 10:11:21.0

原代码:

ExecutorService executor = Executors.newFixedThreadPool(runSize);

修改后代码:

BlockingQueue<Runnable> 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<Runnable> workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler);
  相关解决方案