线程池工具类

线程池的工具类ThreadPoolUtils

import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolUtils {
	// 核心线程数
	private static final int CORE_POOL_SIZE = 50;
	// 最大线程数
	private static final int MAX_POOL_ZISE = 1000;
	// 存活时间
	private static final Long KEEP_ALIVE_TIME = 60L;

	private ThreadPoolUtils() {
	}

	private static class ThreadPoolHolder {
		private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_ZISE,
			KEEP_ALIVE_TIME, TimeUnit.SECONDS, new SynchronousQueue());
	}

	public static ExecutorService getExecutorService() {
		return ThreadPoolHolder.executor;
	}

	public static void execute(Runnable task) {
		ThreadPoolHolder.executor.execute(getExcpThread(task));
	}

	public static void execute(Runnable runnable, Thread.UncaughtExceptionHandler excpHandler) {
		Thread thread = getExcpThread(runnable);
		if (excpHandler != null)
			thread.setUncaughtExceptionHandler(excpHandler);

		ThreadPoolHolder.executor.execute(thread);
	}

	/**
	 * 默认线程异常处理器
	 */
	static class ThreadUncaughtException implements Thread.UncaughtExceptionHandler {
		@Override
		public void uncaughtException(Thread t, Throwable e) {
			// log.error....
		}
	}

	/**
	 * 获取带有异常处理的线程
	 */
	public static Thread getExcpThread(Runnable task) {
		Thread taskThread = null;
		if (task instanceof Thread)
			taskThread = (Thread) task;
		else
			taskThread = new Thread(task);

		if (taskThread.getUncaughtExceptionHandler() == null)
			taskThread.setUncaughtExceptionHandler(new ThreadUncaughtException());
		return taskThread;
	}

}

使用

//写法一
ThreadPoolUtils.execute(new Runnable() {
			@Override
			public void run() {
				try {
					//业务代码
				} catch (IOException e) {
					log.error("异常", e);
				}
			}
		});
//写法二
ThreadPoolUtils.execute(() -> {

			try {
				//业务代码
			} catch (Exception e) {
				log.error("异常", e);
			}
		});

你可能感兴趣的:(线程操作)