newCachedThreadPool: 可缓存线程池,可灵活回收空闲线程
newFixedThreadPool: 定长线程池,可控制线程的最大并发数,超出的线程会在队列中等待
newScheduledThreadPool: 定时线程池,支持定时和周期性任务的执行
newSingleThreadExecutor: 单线程化的线程池,保证所有任务按照指定顺序(先入先出,优先级等)执行
newCachedThreadPool
packagecom.mmall.concurrency.example.threadPool; importlombok.extern.slf4j.Slf4j; importjava.util.concurrent.ExecutorService; importjava.util.concurrent.Executors; publicclassThreadPoolExample1 { publicstaticvoidmain(String[] args) { ExecutorServiceexecutorService=Executors.newCachedThreadPool(); for (inti=0; i<10; i++) { finalintindex=i; executorService.execute(newRunnable() { publicvoidrun() { log.info("task:{}", index); } }); } executorService.shutdown(); } }
// 输出10:57:37.683 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:010:57:37.683 [pool-1-thread-8] INFOc.m.c.example.threadPool.test9-task:710:57:37.683 [pool-1-thread-6] INFOc.m.c.example.threadPool.test9-task:510:57:37.683 [pool-1-thread-10] INFOc.m.c.example.threadPool.test9-task:910:57:37.683 [pool-1-thread-3] INFOc.m.c.example.threadPool.test9-task:210:57:37.683 [pool-1-thread-9] INFOc.m.c.example.threadPool.test9-task:810:57:37.683 [pool-1-thread-4] INFOc.m.c.example.threadPool.test9-task:310:57:37.683 [pool-1-thread-5] INFOc.m.c.example.threadPool.test9-task:410:57:37.683 [pool-1-thread-2] INFOc.m.c.example.threadPool.test9-task:110:57:37.683 [pool-1-thread-7] INFOc.m.c.example.threadPool.test9-task:6Processfinishedwithexitcode0
newFixedThreadPool
packagecom.mmall.concurrency.example.threadPool; importlombok.extern.slf4j.Slf4j; importjava.util.concurrent.ExecutorService; importjava.util.concurrent.Executors; publicclassThreadPoolExample2 { publicstaticvoidmain(String[] args) { ExecutorServiceexecutorService=Executors.newFixedThreadPool(3); for (inti=0; i<10; i++) { finalintindex=i; executorService.execute(newRunnable() { publicvoidrun() { log.info("task:{}", index); } }); } executorService.shutdown(); } }
// 输出11:06:59.613 [pool-1-thread-3] INFOc.m.c.example.threadPool.test9-task:211:06:59.613 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:011:06:59.613 [pool-1-thread-2] INFOc.m.c.example.threadPool.test9-task:111:06:59.617 [pool-1-thread-3] INFOc.m.c.example.threadPool.test9-task:311:06:59.617 [pool-1-thread-3] INFOc.m.c.example.threadPool.test9-task:611:06:59.617 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:411:06:59.617 [pool-1-thread-3] INFOc.m.c.example.threadPool.test9-task:711:06:59.618 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:811:06:59.617 [pool-1-thread-2] INFOc.m.c.example.threadPool.test9-task:511:06:59.618 [pool-1-thread-3] INFOc.m.c.example.threadPool.test9-task:9Processfinishedwithexitcode0
newSingleThreadExecutor
packagecom.mmall.concurrency.example.threadPool; importlombok.extern.slf4j.Slf4j; importjava.util.concurrent.ExecutorService; importjava.util.concurrent.Executors; publicclassThreadPoolExample3 { publicstaticvoidmain(String[] args) { ExecutorServiceexecutorService=Executors.newSingleThreadExecutor(); for (inti=0; i<10; i++) { finalintindex=i; executorService.execute(newRunnable() { publicvoidrun() { log.info("task:{}", index); } }); } executorService.shutdown(); } }
// 输出11:08:19.913 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:011:08:19.918 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:111:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:211:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:311:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:411:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:511:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:611:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:711:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:811:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:9Processfinishedwithexitcode0
newScheduledThreadPool
packagecom.mmall.concurrency.example.threadPool; importlombok.extern.slf4j.Slf4j; importjava.util.Date; importjava.util.Timer; importjava.util.TimerTask; importjava.util.concurrent.Executors; importjava.util.concurrent.ScheduledExecutorService; importjava.util.concurrent.TimeUnit; publicclassThreadPoolExample4 { publicstaticvoidmain(String[] args) { ScheduledExecutorServiceexecutorService=Executors.newScheduledThreadPool(1); // executorService.schedule(new Runnable() {// @Override// public void run() {// log.warn("schedule run");// }// }, 3, TimeUnit.SECONDS);executorService.scheduleAtFixedRate(newRunnable() { publicvoidrun() { log.warn("schedule run"); } }, 1, 3, TimeUnit.SECONDS); // executorService.shutdown();Timertimer=newTimer(); timer.schedule(newTimerTask() { publicvoidrun() { log.warn("timer run"); } }, newDate(), 5*1000); } }
// 输出11:09:04.287 [Timer-0] WARNc.m.c.example.threadPool.test9-timerrun11:09:05.285 [pool-1-thread-1] WARNc.m.c.example.threadPool.test9-schedulerun11:09:08.286 [pool-1-thread-1] WARNc.m.c.example.threadPool.test9-schedulerun11:09:09.286 [Timer-0] WARNc.m.c.example.threadPool.test9-timerrun11:09:11.284 [pool-1-thread-1] WARNc.m.c.example.threadPool.test9-schedulerun11:09:14.286 [pool-1-thread-1] WARNc.m.c.example.threadPool.test9-schedulerun11:09:14.286 [Timer-0] WARNc.m.c.example.threadPool.test9-timerrun11:09:17.287 [pool-1-thread-1] WARNc.m.c.example.threadPool.test9-schedulerun11:09:19.287 [Timer-0] WARNc.m.c.example.threadPool.test9-timerrun11:09:20.286 [pool-1-thread-1] WARNc.m.c.example.threadPool.test9-schedulerun