@Test public void test009ExecutorService取消正在运行的任务() throws Exception { ThreadPoolExecutor executor = RetryUtil.createThreadPoolExecutor(); List<Future<Object>> futures = new ArrayList<Future<Object>>(10); for (int i = 0; i < 10; i++) { Future<Object> f = executor.submit(new Callable<Object>() { @Override public Object call() throws Exception { TimeUnit.SECONDS.sleep(6); return null; } }); futures.add(f); System.out.println("Submit: " + i + ", running tasks: " + executor.getActiveCount()); if (i == 4) { for (Future<Object> future : futures) { future.cancel(true); } System.out.println("Cancel all"); System.out.println("Submit: " + i + ", running tasks: " + executor.getActiveCount()); } TimeUnit.SECONDS.sleep(1); } }
public static ThreadPoolExecutor createThreadPoolExecutor() { return new ThreadPoolExecutor(0, 5, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
RetryUtil { public static ThreadPoolExecutor createThreadPoolExecutor() { return new ThreadPoolExecutor(0, 5, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); } }
RetryUtil { public static ThreadPoolExecutor createThreadPoolExecutor() { return new ThreadPoolExecutor(0, 5, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }  }
RetryUtil { public static ThreadPoolExecutor createThreadPoolExecutor() { return new ThreadPoolExecutor(0, 5, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }  static T executeWithRetry(Callable<T> callable,
                                         int retryTimes,
                                         long sleepTimeInMilliSecond,
                                         boolean exponential); static T executeWithRetry(Callable<T> callable,
                                         int retryTimes,
                                         long sleepTimeInMilliSecond,
                                         boolean exponential,
                                         List<Class<?>> retryExceptionClasss); static T asyncExecuteWithRetry(Callable<T> callable,
                                              int retryTimes,
                                              long sleepTimeInMilliSecond,
                                              boolean exponential,
                                              long timeoutMs,
                                              ThreadPoolExecutor executor); static ThreadPoolExecutor createThreadPoolExecutor(); }
RetryUtil { public static ThreadPoolExecutor createThreadPoolExecutor() { return new ThreadPoolExecutor(0, 5, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }  static T executeWithRetry(Callable<T> callable,
                                         int retryTimes,
                                         long sleepTimeInMilliSecond,
                                         boolean exponential); static T executeWithRetry(Callable<T> callable,
                                         int retryTimes,
                                         long sleepTimeInMilliSecond,
                                         boolean exponential,
                                         List<Class<?>> retryExceptionClasss); static T asyncExecuteWithRetry(Callable<T> callable,
                                              int retryTimes,
                                              long sleepTimeInMilliSecond,
                                              boolean exponential,
                                              long timeoutMs,
                                              ThreadPoolExecutor executor); static ThreadPoolExecutor createThreadPoolExecutor();  }