Custom thread pool tool class

import java.util.concurrent.*;

/**
 * Thread pool tool class:
 * Process tasks that need to be processed asynchronously in the project, such as log services, monitoring services, etc.
 * @author zsc
 * @datetime on November 22, 2017 at 11:04:09 AM
 */
public class ThreadPoolUtil {
	
	/** Tool class, constructor privatization */
	private ThreadPoolUtil() {super();};
	
	// Number of thread pool core threads
	private final static Integer COREPOOLSIZE = 3;
	// maximum number of threads
	private final static Integer MAXIMUMPOOLSIZE = 10;
	// idle thread survival time
	private final static Integer KEEPALIVETIME = 3 * 60;
	// thread waiting queue
	private static BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(10);
	// thread pool object
	private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(COREPOOLSIZE, MAXIMUMPOOLSIZE,
			KEEPALIVETIME, TimeUnit.SECONDS, queue, new ThreadPoolExecutor.AbortPolicy());
	
	/**
	 * Submit a task to the thread pool and return the thread result
	 * @param r
	 * @return
	 */
	public static Future<?> submit(Callable<?> r) {
		return threadPool.submit(r);
	}
	
	/**
	 * Submit a task to the thread pool, don't care about the processing result
	 * @param r
	 */
	public static void execute(Runnable r) {
		threadPool.execute(r);
	}
	
	/** Get the number of threads in the current thread pool */
	public static int getSize() {
		return threadPool.getPoolSize();
	}
	
	/** Get the number of currently active threads */
	public static int getActiveCount() {
		return threadPool.getActiveCount();
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324604421&siteId=291194637