SimpleThreadpool { public void awaitTermination(long timeout) throws TimeoutException { if (this.execute.get()) { throw new IllegalStateException("Threadpool not terminated before awaiting termination"); } long startTime = System.currentTimeMillis(); while (System.currentTimeMillis() - startTime <= timeout) { boolean flag = true; for (Thread thread : threads) { if (thread.isAlive()) { flag = false; break; } } if (flag) { return; } try { Thread.sleep(1); } catch (InterruptedException e) { throw new ThreadpoolException(e); } } throw new TimeoutException("Unable to terminate threadpool within the specified timeout (" + timeout + "ms)"); } private  SimpleThreadpool(int threadCount); static SimpleThreadpool getInstance(); static SimpleThreadpool getInstance(int threadCount); void execute(Runnable runnable); void awaitTermination(long timeout); void awaitTermination(); void terminate(); void stop();  }
@Test public void testAwaitTermination() throws Exception { int runnableCount = 6; SimpleThreadpool threadpool = SimpleThreadpool.getInstance(runnableCount / 2); final AtomicInteger count = new AtomicInteger(0); Runnable r = new Runnable() { @Override public void run() { count.getAndIncrement(); try { Thread.sleep(100); } catch (Exception e) { } } }; for (int i = 0; i < runnableCount; i++) { threadpool.execute(r); } threadpool.stop(); threadpool.awaitTermination(); Assert.assertEquals("Execution should await all runnables", runnableCount, count.get()); }