Executors { public ThreadPoolExecutor makeFixedThreadPoolExecutor(final int nThreads) { ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); manager.register(threadPoolExecutor); return threadPoolExecutor; } private  Executors(ThreadExecutorManager manager); static void init(ThreadExecutorManager manager); static Executors getInstance(); ThreadPoolExecutor makeFixedThreadPoolExecutor(final int nThreads); ThreadPoolExecutor makeFixedThreadPoolExecutor(final String name, final int nThreads); ThreadPoolExecutor makeFixedThreadPoolExecutor(final int nThreads, RejectedExecutionHandler handler); ThreadPoolExecutor makeFixedThreadPoolExecutor(final String name, final int nThreads, RejectedExecutionHandler handler); ThreadPoolExecutor makeSingleThreadPoolExecutor(); ThreadPoolExecutor makeSingleThreadPoolExecutor(final String name); ThreadPoolExecutor makeThreadPoolExecutor(final int corePoolSize, final int maximumPoolSize, final long keepAliveTime, final TimeUnit unit, final BlockingQueue<Runnable> workQueue); ThreadPoolExecutor makeThreadPoolExecutor(final String name, final int corePoolSize, final int maximumPoolSize, final long keepAliveTime, final TimeUnit unit, final BlockingQueue<Runnable> workQueue); ThreadPoolExecutor makeThreadPoolExecutor(final int corePoolSize, final int maximumPoolSize, final long keepAliveTime, final TimeUnit unit, final BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler); ThreadPoolExecutor makeThreadPoolExecutor(final String name, final int corePoolSize, final int maximumPoolSize, final long keepAliveTime, final TimeUnit unit, final BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler);  }
@Test public void makeFixedThreadPoolExecutor() throws Exception { final AtomicInteger singleCount = new AtomicInteger(0); final AtomicInteger fixedCount = new AtomicInteger(0); ThreadPoolExecutor fixed = Executors.getInstance().makeFixedThreadPoolExecutor("fixed",10); ThreadPoolExecutor single = Executors.getInstance().makeSingleThreadPoolExecutor("single"); int needCount = new Random().nextInt(1000); System.out.println("needCount:" + needCount); for (int i = 0; i < needCount; i++) { fixed.execute(new Runnable() { @Override public void run() { fixedCount.incrementAndGet(); System.out.println("来一段：fixed"+ System.currentTimeMillis()); try { TimeUnit.MILLISECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }); single.execute(new Runnable() { @Override public void run() { singleCount.incrementAndGet(); System.out.println("来一段：single"+ System.currentTimeMillis()); try { TimeUnit.MILLISECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }); } manager.destroy(); Assert.assertEquals(needCount, singleCount.get()); Assert.assertEquals(needCount, fixedCount.get()); }