ThreadPools { public static ThreadPoolExecutor newBoundedThreadPool(int maxThreads) { return newBoundedThreadPool(maxThreads, Executors.defaultThreadFactory()); }  static ThreadPoolExecutor newBoundedThreadPool(int maxThreads); static ThreadPoolExecutor newBoundedThreadPool(int maxThreads, ThreadFactory threadFactory); static ThreadPoolExecutor newBoundedThreadPool(int maxThreads, long keepAliveTime, TimeUnit unit,
            ThreadFactory threadFactory); static ThreadPoolExecutor newFixedThreadPool(int nThreads, ThreadFactory threadFactory); static ThreadPoolExecutor newCachedThreadPool(long keepAliveTime, TimeUnit unit,
            ThreadFactory threadFactory);  }
@Test public void testMaxJobsThanWorkers() { final int maxThreads = 10; ThreadFactory tf = new NamedThreadFactory("pool"); ExecutorService tpe = ThreadPools.newBoundedThreadPool(maxThreads, 10L, TimeUnit.MILLISECONDS, tf); for (int i = 0; i < maxThreads * 10; i++) { tpe.execute(new Runnable() { public void run() { new Sleeper(10, Logger.getLogger(Sleeper.class.getName())).sleep(); } }); } tpe.shutdownNow(); }
@Test public void testConcurrency() { final int taskLength = 500; final int maxThreads = 10; final SweetCountDownLatch latch = new SweetCountDownLatch(maxThreads, logger); ThreadFactory tf = new NamedThreadFactory("pool"); ExecutorService tpe = ThreadPools.newBoundedThreadPool(maxThreads, 10L, TimeUnit.MILLISECONDS, tf); for (int i = 0; i < maxThreads; i++) { tpe.execute(new Runnable() { public void run() { latch.countDown(); new Sleeper(taskLength, Logger.getLogger(Sleeper.class.getName())).sleep(); } }); } Assert.assertTrue(latch.await(taskLength / 2, TimeUnit.MILLISECONDS)); for (Thread t : Thread.getAllStackTraces().keySet()) { System.out.println(t); } tpe.shutdownNow(); }