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);