Java examples for java.util.concurrent:ExecutorService
get Simple ExecutorService
//package com.java2s; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class Main { public static void main(String[] argv) throws Exception { int poolSize = 2; System.out.println(getSimpleService(poolSize)); }//w w w . ja va 2s.c om public static final ExecutorService getSimpleService(int poolSize) { final ExecutorService executorService = Executors .newFixedThreadPool(poolSize); addShutdownHook(executorService); return executorService; } private static void addShutdownHook( final ExecutorService executorService) { Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { shutdown(executorService); } }); } public static void shutdown(final ExecutorService executorService) { executorService.shutdown(); try { int timeToWait = 30; if (!executorService.awaitTermination(timeToWait, TimeUnit.SECONDS)) { List<Runnable> executionList = executorService .shutdownNow(); for (Runnable runnable : executionList) { System.out.println("Trying to shutdown task: " + runnable); } } if (!executorService.awaitTermination(timeToWait, TimeUnit.SECONDS)) { } } catch (InterruptedException ex) { executorService.shutdownNow(); Thread.currentThread().interrupt(); } } }