系统自带创建线程池
| 类型 | 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 |
监控
粗糙版
private void printStats(ThreadPoolExecutor threadPool) {Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {log.info("=========================");log.info("Pool Size: {}", threadPool.getPoolSize());log.info("Active Threads: {}", threadPool.getActiveCount());log.info("Number of Tasks Completed: {}", threadPool.getCompletedTaskColog.info("Number of Tasks in Queue: {}", threadPool.getQueue().size())log.info("=========================");}, 0, 1, TimeUnit.SECONDS);}
advanced
- todo
