1. 程式人生 > >每天一例多執行緒[day17]-----Executors框架

每天一例多執行緒[day17]-----Executors框架

  1. /**

  2. * 以下四種底層實現核心都是例項化了一個ThreadPoolExecutor物件返回,其實就是根據以下四種傳遞的具體引數

  3. * 不同而實現的四種不同的功能。

  4. *

  5. * ThreadPoolExecutor來實現以下四種的原因是,當以下四種不滿足我們需求時,我們可以

  6. * 自定義執行緒池。

  7. * 構造引數:

  8. * public ThreadPoolExecutor(

  9. * int corePoolSize,--當前執行緒池核心執行緒數

  10. * int maximumPoolSize,--當前執行緒池最大執行緒數

  11. * long keepAliveTime,--保持活著的空間時間

  12. * TimeUnit unit,--時間單位

  13. * BlockingQueue<Runnable> workQueue,--排隊等待的自定義佇列

  14. * ThreadFactoty threadFactory,

  15. * RejectedExecutionHandler handler--佇列滿以後,其他任務被拒絕執行的方法

  16. * ){.........}

  17. *

  18. */

newFiexedThreadPool

該方法返回一個固定數量的執行緒池,該方法的執行緒數量始終不變,當有一個任務提交時,若執行緒池中空閒,則立即執行,若沒有則會被暫緩在一個任務佇列中等待有空閒的執行緒去執行。

  1. /*

  2. * public static ExecutorService newFixedThreadPool(int nThreads) {

  3. return new ThreadPoolExecutor(nThreads, nThreads,

  4. 0L, TimeUnit.MILLISECONDS,

  5. new LinkedBlockingQueue<Runnable>());

  6. }

  7. * 核心執行緒數=最大執行緒數,空閒時間0s,執行緒執行完直接回收,但是維持執行緒池個數固定,

  8. * 沒有空閒執行緒的話,執行緒會被快取在LinkedBlockingQueue無界阻塞佇列中

  9. */

  10. // ExecutorService pool = Executors.newFixedThreadPool(10);

newSingleThreadExecutor

建立有一個執行緒的執行緒池,若空閒則執行,若沒有空閒則暫緩在任務佇列中。

  1. /*

  2. * 和固定差不多,僅有1個執行緒

  3. * public static ExecutorService newSingleThreadExecutor() {

  4. return new FinalizableDelegatedExecutorService

  5. (new ThreadPoolExecutor(1, 1,

  6. 0L, TimeUnit.MILLISECONDS,

  7. new LinkedBlockingQueue<Runnable>()));

  8. }

  9. */

  10. // ExecutorService pool = Executors.newSingleThreadExecutor();

newCachedThreadPool

返回一個可以根據實際情況調整執行緒個數的執行緒池,不限制最大執行緒數量,若有空閒的執行緒則執行任務,若無任務則不建立執行緒,並且空閒執行緒會在60s後自動回收。

  1. /**

  2. * 初始化時核心執行緒0,不限制執行緒最大數,

  3. * 只要任務來了就建立一個執行緒並且執行任務,使用SynchronousQueue,沒有任何容量。

  4. * 執行緒最大空閒時間60s,如果執行緒在60s內沒有任務到來就回收執行緒。如果59s的時候來了,就去執行

  5. * 任務不會回收執行緒。

  6. *

  7. * public static ExecutorService newCachedThreadPool() {

  8. return new ThreadPoolExecutor(0, Integer.MAX_VALUE,

  9. 60L, TimeUnit.SECONDS,

  10. new SynchronousQueue<Runnable>());

  11. }

  12. */

  13. // ExecutorService pool = Executors.newCachedThreadPool();

newScheduledThreadPool

該方法返回一個ScheduledExecutorService物件,但該執行緒池可以指定執行緒的數量。

  1. /**

  2. * 初始化指定的核心執行緒數,且沒有最大執行緒數限制,空閒時間0s,執行完任務就回收執行緒,

  3. * 執行緒沒有空閒時任務到來,放到DelayedWorkQueue中。只有等延遲時間到了,任務才能被取出執行。

  4. *

  5. *

  6. * public ScheduledThreadPoolExecutor(int corePoolSize) {

  7. super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,

  8. new DelayedWorkQueue());

  9. }

  10. */

  11. // ExecutorService pool = Executors.newScheduledThreadPool(10);

舉例:

  1. import java.util.concurrent.Executors;

  2. import java.util.concurrent.ScheduledExecutorService;

  3. import java.util.concurrent.ScheduledFuture;

  4. import java.util.concurrent.TimeUnit;

  5. class Temp extends Thread {

  6. public void run() {

  7. System.out.println("run");

  8. }

  9. }

  10. public class ScheduledJob {

  11. public static void main(String args[]) throws Exception {

  12. Temp command = new Temp();

  13. ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

  14. /**

  15. * scheduleWithFixedDelay(

  16. * Runnable command, 具體執行緒任務

  17. * long initialDelay, 初始化延遲執行時間

  18. * long delay, 輪詢執行時間,每隔delay時間執行一次

  19. * TimeUnit unit 單位

  20. * )

  21. *

  22. */

  23. //5s後,每隔1s執行一次command

  24. ScheduledFuture<?> scheduleTask =

  25. scheduler.scheduleWithFixedDelay(command, 5, 1, TimeUnit.SECONDS);

  26. }

  27. }

列印:

  1. run

  2. run

  3. run

  4. run