Code example for ExecutorService

Methods: awaitTermination, shutdown, shutdownNow

0
	/** 
	 * Shuts down the thread pool and waits for termination. 
	 */ 
	private void shutdownAndAwaitTermination() { 
		// Disable new tasks from being submitted. 
		threadPool.shutdown(); 
		try { 
			// Wait a while for existing tasks to terminate. 
			if (!threadPool.awaitTermination(60, TimeUnit.SECONDS)) {
				// Cancel currently executing tasks. 
				threadPool.shutdownNow();
				// Wait a while for tasks to respond to being cancelled. 
				if (!threadPool.awaitTermination(60, TimeUnit.SECONDS))
					System.err.println("Pool did not terminate");
 
			} 
		} catch (InterruptedException ie) {
			// (Re-)Cancel if current thread also interrupted. 
			threadPool.shutdownNow();
			// Preserve interrupt status. 
			Thread.currentThread().interrupt();
		} 
	}