創建線程池
java.util.concurrent.Executors
public class Executors
static ExecutorService newCachedThreadPool()
- 可以重用之前創建的、現處於空閒狀態的線程。
- 如果線程池中沒有空閒線程以執行任務,則創建一個並添加到線程池中。
- 線程空閒 60秒 後,自動銷燬之。
static ExecutorService newFixedThreadPool(int nThreads)
- 任意時刻,線程池中最多隻會有 nThreads 個線程。
- 如果沒有空閒線程以執行任務,則會將任務放到隊列中等待。
- 如果線程因出錯而終止,則會有一個新線程代替它以執行其他任務。
- 線程池中的線程會在顯式調用 shutdown() 之後銷燬。
static ExecutorService newSingleThreadExecutor()
- 線程池中只有一個線程。
- 如果線程不空閒,則會將新任務放到隊列中等待。確保順序執行提交的任務。
- 如果線程因出錯而終止,則會有一個新線程代替它以執行其他任務。
static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
- 線程池中的線程可以在給定時延後、或週期性地執行任務。
- corePoolSize :線程池中的線程數。
static ScheduledExecutorService newSingleThreadScheduledExecutor()
- 線程池中的線程可以在給定時延後、或週期性地執行任務。
- 線程池中只有一個線程。
- 如果線程不空閒,則會將新任務放到隊列中等待。確保順序執行提交的任務。
- 如果線程因出錯而終止,則會有一個新線程代替它以執行其他任務。
// 使用所有可用的處理器數作爲它的並行級別。
static ExecutorService newWorkStealingPool()
// parallelism:指定並行級別。
static ExecutorService newWorkStealingPool(int parallelism)
-
工作竊取線程池:假設共有三個線程同時執行, A, B, C,當A,B線程尚未處理完任務,而C已經處理完畢,則C線程會從A或者B中竊取任務執行,這就叫工作竊取。
假如A線程中的隊列裏面分配了5個任務,而B線程的隊列中分配了1個任務,當B線程執行完任務後,它會主動的去A線程中竊取其他的任務進行執行。 -
並行級別對應於主動參與或可參與任務處理的線程的最大數量。
-
不保證順序處理提交的任務。
Executor
java.util.concurrent.Executor
public interface Executor
- 執行提交的任務。
- 將任務提交和任務如何執行解耦。
void execute(Runnable command)
在未來的某個時間執行給定的任務。該任務可以在新線程、線程池、調用線程中執行,具體由執行器實現決定。
ExecutorService
java.util.concurrent.ExecutorService
public interface ExecutorService extends Executor
提交任務
// 任務成功執行完畢時,調用 Future 的 get() 方法將返回任務的返回值
<T> Future<T> submit(Callable<T> task)
// 任務成功執行完畢時,調用 Future 的 get() 方法將返回 null
Future<?> submit(Runnable task)
// 任務成功執行完畢時,調用 Future 的 get() 方法將返回 result
<T> Future<T> submit(Runnable task, T result)
關閉執行器
void shutdown()
- 關閉執行器。執行器會等待之前提交的任務執行完畢,但不會再接受新提交的任務。
- 立即返回,不會等待之前提交的任務執行完畢。
List<Runnable> shutdownNow()
- 嘗試停止所有正在執行的任務。停止處理等待的任務,並返回等待執行的任務列表。
- 不保證能夠成功停止正在執行的線程。
- 立即返回,不會等待活動線程停止執行。
boolean awaitTermination(long timeout, TimeUnit unit)
- 阻塞,直到所有任務在關閉請求後完成執行,或發生超時,或當前線程中斷(以先發生者爲準)。
boolean isShutdown()
- 若執行器已被關閉,則返回 true
boolean isTerminated()
- 如果關閉後所有任務都已完成,則返回true。注意,除非首先調用 shutdown 或 shutdownNow,否則 isTerminated 永遠不會返回 true。
ScheduledExecutorService
java.util.concurrent.ScheduledExecutorService
public interface ScheduledExecutorService extends ExecutorService
<V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
- 在 delay unit 時延後,執行 callable 任務。
ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
- 在 delay unit 時延後,執行 command 任務。
ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
- 在 initialDelay unit 時延後,開始執行 command 任務。並且以後每隔 period unit 時長便再次執行。即開始執行任務的時間爲:initialDelay、initialDelay+period、initialDelay + 2 * period …
- 如果中間某次執行出了錯,則不會再繼續週期性地執行。
- 如果執行任務耗時大於 period unit,則會延遲下次執行,而不會併發執行。
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
- 在 initialDelay unit 時延後,開始執行 command 任務。並且以後在上次任務執行完畢後的 delay unit 時間後再次執行。即開始執行任務的時間爲:initialDelay、initialDelay+上個任務執行完畢所需時間+delay…
- 如果中間某次執行出了錯,則不會再繼續執行。