使用Jdk
// 手动构建线程池 ExecutorService executor = new ThreadPoolExecutor( 4, 8, 60, TimeUnit.SECONDS, new SynchronousQueue<>(), new ThreadFactoryBuilder() .setDaemon(false) .setNameFormat("evenExecutor-%d") .build(), new ThreadPoolExecutor.AbortPolicy());
使用Spring
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(4); executor.setMaxPoolSize(8); executor.setKeepAliveSeconds(60); executor.setThreadNamePrefix("Executor-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());