public ExecutorService newThreadPool(int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit timeUnit, int maxQueueSize, boolean allowCoreThreadTimeOut, RejectedExecutionHandler rejectedExecutionHandler, ThreadFactory threadFactory) throws IllegalArgumentException { // the core pool size must be 0 or higher if (corePoolSize < 0) { throw new IllegalArgumentException("CorePoolSize must be >= 0, was " + corePoolSize); } // validate max >= core if (maxPoolSize < corePoolSize) { throw new IllegalArgumentException("MaxPoolSize must be >= corePoolSize, was " + maxPoolSize + " >= " + corePoolSize); } BlockingQueue<Runnable> workQueue; if (corePoolSize == 0 && maxQueueSize <= 0) { // use a synchronous queue for direct-handover (no tasks stored on the queue) workQueue = new SynchronousQueue<Runnable>(); // and force 1 as pool size to be able to create the thread pool by the JDK corePoolSize = 1; maxPoolSize = 1; } else if (maxQueueSize <= 0) { // use a synchronous queue for direct-handover (no tasks stored on the queue) workQueue = new SynchronousQueue<Runnable>(); } else { // bounded task queue to store tasks on the queue workQueue = new LinkedBlockingQueue<Runnable>(maxQueueSize); } ThreadPoolExecutor answer = new RejectableThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, timeUnit, workQueue); answer.setThreadFactory(threadFactory); answer.allowCoreThreadTimeOut(allowCoreThreadTimeOut); if (rejectedExecutionHandler == null) { rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy(); } answer.setRejectedExecutionHandler(rejectedExecutionHandler); return answer; }
public ExecutorService newThreadPool(int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit timeUnit, int maxQueueSize, boolean allowCoreThreadTimeOut, RejectedExecutionHandler rejectedExecutionHandler, ThreadFactory threadFactory) throws IllegalArgumentException { if(corePoolSize < 0) { throw new IllegalArgumentException("CorePoolSize must be >= 0, was " + corePoolSize); } else if(maxPoolSize < corePoolSize) { throw new IllegalArgumentException("MaxPoolSize must be >= corePoolSize, was " + maxPoolSize + " >= " + corePoolSize); } else { Object workQueue; if(corePoolSize == 0 && maxQueueSize <= 0) { workQueue = new SynchronousQueue(); corePoolSize = 1; maxPoolSize = 1; } else if(maxQueueSize <= 0) { workQueue = new SynchronousQueue(); } else { workQueue = new LinkedBlockingQueue(maxQueueSize); } RejectableThreadPoolExecutor answer = new RejectableThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, timeUnit, (BlockingQueue)workQueue); answer.setThreadFactory(managedThreadFactory); answer.allowCoreThreadTimeOut(allowCoreThreadTimeOut); if(rejectedExecutionHandler == null) { rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy(); } answer.setRejectedExecutionHandler(rejectedExecutionHandler); return answer; } }
private ExecutorService createTestExecutorService(final RejectedExecutionHandler rejectedExecutionHandler) { return new RejectableThreadPoolExecutor(1, 1, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1), rejectedExecutionHandler); }