线程池 - 图1

系统自带创建线程池

类型 FiexedThreadPool SingleThreadPool CacheThreadPool ScheduleThreadPool
corePoolSize 设置 1 0 设置
maximumPoolSize == corePoolSize 1 Integer.MAX_VALUE Integer.MAX_VALUE
keepAliveTime 0 0 60 0
TimeUnit TimeUnit.Mills TimeUnit.Mills TimeUnit.SECOND TimeUnit.NANOSECONDS
WorkingQueue LinkedBlockingQueue LinkedBlockingQueue SynchroniousQueue DelayedWorkQueue

监控

  • 粗糙版

    1. private void printStats(ThreadPoolExecutor threadPool) {
    2. Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
    3. log.info("=========================");
    4. log.info("Pool Size: {}", threadPool.getPoolSize());
    5. log.info("Active Threads: {}", threadPool.getActiveCount());
    6. log.info("Number of Tasks Completed: {}", threadPool.getCompletedTaskCo
    7. log.info("Number of Tasks in Queue: {}", threadPool.getQueue().size())
    8. log.info("=========================");
    9. }, 0, 1, TimeUnit.SECONDS);
    10. }
  • advanced

    • todo