ExecutorUtil { public static void shutdownNow(Executor executor, final int timeout) { if (!(executor instanceof ExecutorService) || isTerminated(executor)) { return; } final ExecutorService es = (ExecutorService) executor; try { es.shutdownNow(); } catch (SecurityException ex2) { return; } catch (NullPointerException ex2) { return; } try { es.awaitTermination(timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } if (!isTerminated(es)) { newThreadToCloseExecutor(es); } }  static boolean isTerminated(Executor executor); static void gracefulShutdown(Executor executor, int timeout); static void shutdownNow(Executor executor, final int timeout); static URL setThreadName(URL url, String defaultName);  }
@Test public void testShutdownNow() throws Exception { ExecutorService executor = Mockito.mock(ExecutorService.class); when(executor.isTerminated()).thenReturn(false, true); ExecutorUtil.shutdownNow(executor, 20); verify(executor).shutdownNow(); verify(executor).awaitTermination(20, TimeUnit.MILLISECONDS); }