ThreadPoolUtils { public static ThreadPoolExecutor newCachedThreadPool(int corePoolSize, int maximumPoolSize) { return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, DateUtils.MILLISECONDS_PER_MINUTE, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>()); }  static ThreadPoolExecutor newFixedThreadPool(int corePoolSize); static ThreadPoolExecutor newFixedThreadPool(int corePoolSize,
                                                        BlockingQueue<Runnable> queue); static ThreadPoolExecutor newFixedThreadPool(int corePoolSize,
                                                        BlockingQueue<Runnable> queue,
                                                        ThreadFactory threadFactory); static ThreadPoolExecutor newFixedThreadPool(int corePoolSize,
                                                        BlockingQueue<Runnable> queue,
                                                        ThreadFactory threadFactory,
                                                        RejectedExecutionHandler handler); static ThreadPoolExecutor newCachedThreadPool(int corePoolSize,
                                                         int maximumPoolSize); static ThreadPoolExecutor newCachedThreadPool(int corePoolSize,
                                                         int maximumPoolSize,
                                                         BlockingQueue<Runnable> queue); static ThreadPoolExecutor newCachedThreadPool(int corePoolSize,
                                                         int maximumPoolSize,
                                                         BlockingQueue<Runnable> queue,
                                                         ThreadFactory threadFactory); static ThreadPoolExecutor newCachedThreadPool(int corePoolSize,
                                                         int maximumPoolSize,
                                                         BlockingQueue<Runnable> queue,
                                                         ThreadFactory threadFactory,
                                                         RejectedExecutionHandler handler); static ThreadPoolExecutor newCachedThreadPool(int corePoolSize,
                                                         int maximumPoolSize,
                                                         int keepAliveTime,
                                                         BlockingQueue<Runnable> queue,
                                                         ThreadFactory threadFactory,
                                                         RejectedExecutionHandler handler); static BlockingQueue<Runnable> buildQueue(int size); static BlockingQueue<Runnable> buildQueue(int size, boolean isPriority); static int THREAD_PRIORITY_NORMAL; static int THREAD_PRIORITY_HIGH; static int THREAD_PRIORITY_LOW; }
@Test public void newCachedThreadPool() throws Exception { ThreadFactory factory = new NamedThreadFactory("xxx"); ThreadPoolExecutor executor = ThreadPoolUtils.newCachedThreadPool(10, 20); Assert.assertEquals(executor.getCorePoolSize(), 10); Assert.assertEquals(executor.getMaximumPoolSize(), 20); }
@Test public void newCachedThreadPool1() throws Exception { BlockingQueue<Runnable> queue = new SynchronousQueue<Runnable>(); ThreadFactory factory = new NamedThreadFactory("xxx"); ThreadPoolExecutor executor = ThreadPoolUtils.newCachedThreadPool(10, 20, queue); Assert.assertEquals(executor.getCorePoolSize(), 10); Assert.assertEquals(executor.getMaximumPoolSize(), 20); Assert.assertEquals(executor.getQueue(), queue); }
@Test public void newCachedThreadPool2() throws Exception { BlockingQueue<Runnable> queue = new SynchronousQueue<Runnable>(); ThreadFactory factory = new NamedThreadFactory("xxx"); ThreadPoolExecutor executor = ThreadPoolUtils.newCachedThreadPool(10, 20, queue, factory); Assert.assertEquals(executor.getCorePoolSize(), 10); Assert.assertEquals(executor.getMaximumPoolSize(), 20); Assert.assertEquals(executor.getQueue(), queue); Assert.assertEquals(executor.getThreadFactory(), factory); }
@Test public void newCachedThreadPool3() throws Exception { BlockingQueue<Runnable> queue = new SynchronousQueue<Runnable>(); RejectedExecutionHandler handler = new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { } }; ThreadFactory factory = new NamedThreadFactory("xxx"); ThreadPoolExecutor executor = ThreadPoolUtils.newCachedThreadPool(10, 20, queue, factory, handler); Assert.assertEquals(executor.getCorePoolSize(), 10); Assert.assertEquals(executor.getMaximumPoolSize(), 20); Assert.assertEquals(executor.getQueue(), queue); Assert.assertEquals(executor.getThreadFactory(), factory); Assert.assertEquals(executor.getRejectedExecutionHandler(), handler); }
@Test public void newCachedThreadPool4() throws Exception { BlockingQueue<Runnable> queue = new SynchronousQueue<Runnable>(); RejectedExecutionHandler handler = new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { } }; ThreadFactory factory = new NamedThreadFactory("xxx"); ThreadPoolExecutor executor = ThreadPoolUtils.newCachedThreadPool(10, 20, 45678, queue, factory, handler); Assert.assertEquals(executor.getCorePoolSize(), 10); Assert.assertEquals(executor.getMaximumPoolSize(), 20); Assert.assertEquals(executor.getQueue(), queue); Assert.assertEquals(executor.getKeepAliveTime(TimeUnit.MILLISECONDS), 45678); Assert.assertEquals(executor.getThreadFactory(), factory); Assert.assertEquals(executor.getRejectedExecutionHandler(), handler); }