使用Jdk

  1. // 手动构建线程池
  2. ExecutorService executor = new ThreadPoolExecutor(
  3. 4, 8, 60, TimeUnit.SECONDS,
  4. new SynchronousQueue<>(),
  5. new ThreadFactoryBuilder()
  6. .setDaemon(false)
  7. .setNameFormat("evenExecutor-%d")
  8. .build(), new ThreadPoolExecutor.AbortPolicy());

使用Spring

  1. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  2. executor.setCorePoolSize(4);
  3. executor.setMaxPoolSize(8);
  4. executor.setKeepAliveSeconds(60);
  5. executor.setThreadNamePrefix("Executor-");
  6. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());