SimpleThreadpool { public void terminate() { runnables.clear(); stop(); } 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 testTerminate() throws Exception { int runnableCount = 10; 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(200); } catch (Exception e) { } } }; for (int i = 0; i < runnableCount; i++) { threadpool.execute(r); } threadpool.terminate(); threadpool.awaitTermination(300); Assert.assertFalse("Threadpool should terminate without executing pending runnables", runnableCount == count.get()); }