Code example for ExecutorService

Methods: awaitTerminationshutdownshutdownNow

	 * 如果仍人超時,則強制退出. 
	 * 另对在shutdown时线程本身被调用中断做了处理. 
	public static void gracefulShutdown(ExecutorService pool, int shutdownTimeout, int shutdownNowTimeout, 
			TimeUnit timeUnit) { 
		pool.shutdown(); // Disable new tasks from being submitted 
		try { 
			// Wait a while for existing tasks to terminate 
			if (!pool.awaitTermination(shutdownTimeout, timeUnit)) { 
				pool.shutdownNow(); // Cancel currently executing tasks 
				// Wait a while for tasks to respond to being cancelled 
				if (!pool.awaitTermination(shutdownNowTimeout, timeUnit)) { 
					System.err.println("Pool did not terminated"); 
		} catch (InterruptedException ie) { 
			// (Re-)Cancel if current thread also interrupted 
			// Preserve interrupt status 
Experience pair programming with AI  Get Codota for Java