Code example for ThreadFactoryBuilder

0
	/** 
	 * 创建ThreadFactory,使得创建的线程有自己的名字而不是默认的"pool-x-thread-y", 
	 * 在用threaddump查看线程时特别有用。 格式如"mythread-%d",使用了Guava的工具类 
	 */ 
	public static ThreadFactory buildJobFactory(String nameFormat) {
		return new ThreadFactoryBuilder().setNameFormat(nameFormat).build();
	} 
 
	/** 
	 * 按照ExecutorService JavaDoc示例代码编写的Graceful Shutdown方法. 
	 * 先使用shutdown, 停止接收新任务并尝试完成所有已存在任务. 
	 * 如果超时, 则调用shutdownNow, 取消在workQueue中Pending的任务,并中断所有阻塞函数. 
	 * 如果仍然超時,則強制退出. 
	 * 另对在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