這篇文章大部分都是直接摘抄自《實戰Java高并發程序設計》,基本上就是一篇筆記,用于以后忘了的時候可以回顧。
框架提供的ExecutorService
Executors框架提供了各種類型的線程池,主要有以下工廠方法:
public static ExecutorService newFixedThreadPool(int nThreads)
public static ExecutorService newSingleThreadExecutor()
public static ExecutorService newCachedThreadPool()
public static ScheduledExecutorService newSingleThreadScheduledExecutor()
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
newFixedThreadPool()方法. 該方法返回一個固定線程數量的線程池,該線程池中的線程數量始終不變,當有一個新任務時,線程池中若有空閑線程,則立即執行,若沒有,則新任務會被暫時存在一個隊列中,得有空閑線程時,便處理在任務隊列中的任務
newSingleThreadExecutor()方法,改方法返回一個只有一個線程的線程池,若多余一個任務被提交到該線程池,任務會被保存在一個隊伍隊列,帶線程空閑,按先入先出的順序執行隊列中的任務,
newCachedThreadPool()方法,該方法返回一個可根據實際情況調整線程數量的線程池.線程池數量是不確定的,但若有空閑線程可以復用,則會優先使用可以復用的線程,若所有線程均在工作,又有新的任務提交,則會創建新的線程處理任務,所有線程在當前任務執行完畢后,將返回線程池進行復用,
newSingleThreadScheduledExecutor()方法: 改方法返回一個ScheduledExecutorService對象,線程池大小為1 這個接口在ExecutorService接口之上拓展了在給定時間執行某任務的功能,如在某個固定的延時之后執行,或者周期性執行某個任務.
newScheduledThreadPool()方法:改方法也返回一個ScheduledExecutorService對象 但改線程池可以指定線程數量
前面三個工廠方法創建的ExecutorService只需要使用ExecutorService.execute()方法或者submit()方法將需要執行的任務傳入即可,這里就不細講了。關于這兩個方法的差異我會在后面細說,這里也不展開討論了。
后面兩個工廠方法會創建ScheduledExecutorService。它有會多出下面三個schedule方法用于延遲執行任務:
public ScheduledFuture<?> schedule(Runnable command,
long delay, TimeUnit unit);
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
schedule()方法會在給定時間,對方法進行一次調度。scheduleAtFixedRate()方法和scheduleWithFixedDelay()會對任務進行周期性調度。但兩者有一點小小的差別:
對于FixedRate方式來說,任務調度的頻率是一樣的。它是以上一個任務開始執行時間為起點,之后的period時間,調度下一次任務。而FixDelay則是在上一個任務結束后,再經過delay時間進行任務調度。
ThreadPoolExecutor
對于Executors.newFixedThreadPool()、Executors.newSingleThreadExecutor()、Executors.newCachedThreadPool()這幾個方法雖然創建的線程池的功能特點完全不一樣,但是他們其實都是使用了ThreadPoolExecutor實現:
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
ThreadPoolExecutor的最重要的構造函數如下:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
- corePoolSize: 指定了線程池中的線程數量
- maximumPoolSize: 指定了線程池中的最大線程數量
- keepAliveTime: 當線程池線程數量超過corePoolSize時,多余的空閑線程的存活時間。即,超過corePoolSize的空閑線程,在多長的時間內,會被銷毀。
- unit: keepAliveTime的時間單位
- workQueue: 被提交但未被執行的任務
- threadFactory: 線程工廠,用于創建線程,一般用默認即可
- handler: 拒絕策略。但任務太多來不及處理,如何拒絕任務
以上參數中,大部分都很簡單,只有workQueue和handler需要說一下。
內置的BlockingQueue有下面幾種:
SynchronousQueue: 一個沒有容量的隊列。使用SynchronousQueue,提交的任務不會真正的被保存,而總是將新任務提交給線程執行。如果沒有空閑線程,就創建新線程,如果線程數量已經到達最大值,則執行拒絕策略
ArrayBlockingQueue: 有界任務隊列,若有新的任務需要執行,如果實際線程數少于corePoolSize則創建新的線程,如果大于corePoolSize,就會放入ArrayBlockingQueue中,如果ArrayBlockingQueue已滿,在總線程數不大于maximumPoolSize的情況下會創建新線程,否則就執行拒絕策略
LinkedBlockingQueue: 無界任務隊列,若有新的任務需要執行,如果實際線程數少于corePoolSize則創建新的線程,如果大于corePoolSize,就會放入LinkedBlockingQueue中等待
PriorityBlockingQueue: 它是一個特殊的無界隊列,可以設定任務的優先級
而內置的拒絕策略又有下面幾種:
- AbortPolicy策略: 該策略會直接拋出異常,阻止系統正常工作
- CallerRunsPolicy策略: 只要線程池沒有關閉,該策略直接在調用者線程中運行被拒絕的任務。(使用這個策略可能導致在主線程執行耗時操作)
- DiscardOldestPolicy策略: 該策略丟棄一個最老的任務,并嘗試重新提交任務
- DiscardPolicy策略: 該策略默默丟棄拒絕的任務,不做任何處理。
線程池任務調度的邏輯如下圖所示:
execute和submit的區別
ExecutorService.execute()和ExecutorService.submit()都可以提交任務去異步執行,但是它們之間有什么區別呢?
void execute(Runnable command);
Future<?> submit(Runnable task);
<T> Future<T> submit(Callable<T> task);
- 返回值
ExecutorService.execute()沒有返回值,只能簡單的提交Runnable給線程池去運行
ExecutorService.submit(),有返回值,可以獲得一個Future
- 異常
ExecutorService.execute()的異常機制和普通線程的異常機制一樣,必須用try、catch來捕獲異常。如果沒有捕獲一些運行時異常,也會打印出堆棧信息:
Executors.newCachedThreadPool().execute(
new Runnable() {
@Override
public void run() {
int i = 1 / 0;
}
}
);
Exception in thread "pool-1-thread-1" java.lang.ArithmeticException: / by zero
ExecutorService.submit()的異常會被吃掉,下面的代碼的異常會被默默吃掉,沒有堆棧信息的打印:
Executors.newCachedThreadPool().submit(
new Runnable() {
@Override
public void run() {
int i = 1 / 0;
}
}
);
但是我們可以調用Future.get()方法,這樣當拋出異常的時候系統也會打印堆棧:
Future future = Executors.newCachedThreadPool().submit(
new Runnable() {
@Override
public void run() {
int i = 1 / 0;
}
}
);
future.get();
需要注意的是Future.get()是阻塞的,需要需要等待線程執行完畢才會返回,所以我們可以用這個方法獲得Callable.call()的返回值:
Future<Integer> future = Executors.newCachedThreadPool().submit(
new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return 123;
}
}
);
System.out.println(future.get());