newCachedThreadPool建立一个可缓存线程池,若是线程池长度超过处理须要,可灵活回收空闲线程,若无可回收,则新建线程。 newFixedThreadPool 建立一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。 newScheduledThreadPool 建立一个定长线程池,支持定时及周期性任务执行。 newSingleThreadExecutor建立一个单线程化的线程池,它只会用惟一的工做线程来执行任务,保证全部任务按照指定顺序(FIFO, LIFO, 优先级)执行。spring
每一个线程例子:apache
ExecutorService e = Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++) { e.submit(() -> { String name = Thread.currentThread().getName(); System.out.println("使用CachedThread建立线程:" + name); }); } ExecutorService executorService = Executors.newFixedThreadPool(3); for (int j = 0; j < 5; j++) { executorService.submit(() -> { String name = Thread.currentThread().getName(); System.out.println("使用FixedThread建立线程:" + name); }); } ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3); for (int k = 0; k < 5; k++) { scheduledExecutorService.schedule(() -> { String name = Thread.currentThread().getName(); System.out.println("使用ScheduledThread建立线程:" + name); System.out.println(System.currentTimeMillis()); }, 2, TimeUnit.SECONDS); } ExecutorService singleTHread = Executors.newSingleThreadExecutor(); for (int n = 0; n < 5; n++) { singleTHread.submit(() -> { String name = Thread.currentThread().getName(); System.out.println("使用SingleThread建立线程:" + name); }); } 输出: 使用CachedThread建立线程:pool-1-thread-1 使用CachedThread建立线程:pool-1-thread-1 使用CachedThread建立线程:pool-1-thread-1 使用CachedThread建立线程:pool-1-thread-1 使用CachedThread建立线程:pool-1-thread-1 使用FixedThread建立线程:pool-2-thread-1 使用FixedThread建立线程:pool-2-thread-2 使用FixedThread建立线程:pool-2-thread-3 使用FixedThread建立线程:pool-2-thread-1 使用FixedThread建立线程:pool-2-thread-2 使用SingleThread建立线程:pool-4-thread-1 使用SingleThread建立线程:pool-4-thread-1 使用SingleThread建立线程:pool-4-thread-1 使用SingleThread建立线程:pool-4-thread-1 使用SingleThread建立线程:pool-4-thread-1 使用ScheduledThread建立线程:pool-3-thread-1 使用ScheduledThread建立线程:pool-3-thread-3 1550728498308 使用ScheduledThread建立线程:pool-3-thread-2 1550728498308 使用ScheduledThread建立线程:pool-3-thread-3 1550728498308 1550728498308 使用ScheduledThread建立线程:pool-3-thread-2 1550728498309
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build(); ExecutorService pool = new ThreadPoolExecutor(2, 8, 1L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(20), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy()); for (int n = 0; n < 20; n++) { pool.submit(() -> { String name = Thread.currentThread().getName(); System.out.println("使用ThreadPoolExecutor建立线程:" + name); System.out.println(pool.toString()); }); }
阿里编程规范提示: 线程池不容许使用Executors去建立,而是经过ThreadPoolExecutor的方式,这样的处理方式让写的同窗更加明确线程池的运行规则,规避资源耗尽的风险。 说明:Executors各个方法的弊端: 1)newFixedThreadPool和newSingleThreadExecutor: 主要问题是堆积的请求处理队列可能会耗费很是大的内存,甚至OOM。 2)newCachedThreadPool和newScheduledThreadPool: 主要问题是线程数最大数是Integer.MAX_VALUE,可能会建立数量很是多的线程,甚至OOM。编程
Positive example 1:缓存
//org.apache.commons.lang3.concurrent.BasicThreadFactory ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
Positive example 2:并发
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder() .setNameFormat("demo-pool-%d").build(); //Common Thread Pool ExecutorService pool = new ThreadPoolExecutor(5, 200, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy()); pool.execute(()-> System.out.println(Thread.currentThread().getName())); pool.shutdown();//gracefully shutdown
Positive example 3:ui
<bean id="userThreadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="10" /> <property name="maxPoolSize" value="100" /> <property name="queueCapacity" value="2000" /> <property name="threadFactory" value= threadFactory /> <property name="rejectedExecutionHandler"> <ref local="rejectedExecutionHandler" /> </property> </bean> //in code userThreadPool.execute(thread);