java線(xiàn)程池核心API源碼詳細(xì)分析
概述
線(xiàn)程池是一種多線(xiàn)程處理形式,處理過(guò)程中將任務(wù)添加到隊(duì)列,然后在創(chuàng)建線(xiàn)程后自動(dòng)啟動(dòng)這些任務(wù)。線(xiàn)程池線(xiàn)程都是后臺(tái)線(xiàn)程。每個(gè)線(xiàn)程都使用默認(rèn)的堆棧大小,以默認(rèn)的優(yōu)先級(jí)運(yùn)行,并處于多線(xiàn)程單元中。如果某個(gè)線(xiàn)程在托管代碼中空閑(如正在等待某個(gè)事件),則線(xiàn)程池將插入另一個(gè)輔助線(xiàn)程來(lái)使所有處理器保持繁忙。如果所有線(xiàn)程池線(xiàn)程都始終保持繁忙,但隊(duì)列中包含掛起的工作,則線(xiàn)程池將在一段時(shí)間后創(chuàng)建另一個(gè)輔助線(xiàn)程但線(xiàn)程的數(shù)目永遠(yuǎn)不會(huì)超過(guò)最大值。超過(guò)最大值的線(xiàn)程可以排隊(duì),但他們要等到其他線(xiàn)程完成后才啟動(dòng)。
jdk中當(dāng)然也提供了一些操作線(xiàn)程池的API。
java中操作線(xiàn)程池的api位于java.util.concurrent;包中。
常見(jiàn)的核心接口和實(shí)現(xiàn)類(lèi)包括Executor接口、ExecutorService接口、ScheduledExecutorService接口、ThreadPoolExecutor實(shí)現(xiàn)類(lèi)、ScheduledThreadPoolExecutor實(shí)現(xiàn)類(lèi)等。
Executor接口
最上層的接口,定義了執(zhí)行任務(wù)的execute()方法。

ExecutorService接口
繼承了Executor接口,擴(kuò)展了Callable、Future、關(guān)閉方法。

ScheduledExecutorService接口
繼承了ExecutorService接口,增加了定時(shí)任務(wù)相關(guān)的方法。

ThreadPoolExecutor實(shí)現(xiàn)類(lèi)
基礎(chǔ)、標(biāo)準(zhǔn)的線(xiàn)程池實(shí)現(xiàn)。
ScheduledThreadPoolExecutor實(shí)現(xiàn)類(lèi)
繼承了ThreadPoolExecutor,實(shí)現(xiàn)了ScheduledExecutorService中定時(shí)任務(wù)相關(guān)的方法。
他們之間的繼承實(shí)現(xiàn)關(guān)系圖如下:

源碼分析
下面對(duì)Executor接口、ExecutorService接口、ScheduledExecutorService接口、ThreadPoolExecutor實(shí)現(xiàn)類(lèi)、ScheduledThreadPoolExecutor實(shí)現(xiàn)類(lèi)這幾個(gè)核心api的源碼進(jìn)行分析。
Executor
public interface Executor {
/**
* Executes the given command at some time in the future. The command
* may execute in a new thread, in a pooled thread, or in the calling
* thread, at the discretion of the {@code Executor} implementation.
*
* @param command the runnable task
* @throws RejectedExecutionException if this task cannot be
* accepted for execution
* @throws NullPointerException if command is null
*/
void execute(Runnable command);
}
Executor是最上層的接口,該接口提供了一個(gè)execute()方法,用于執(zhí)行指定的Runnable任務(wù)。
ExecutorService
ExecutorService接口繼承至Executor接口,擴(kuò)展了Callable、Future、關(guān)閉等方法。
public interface ExecutorService extends Executor
定義的方法
//啟動(dòng)有序關(guān)閉,先前已經(jīng)執(zhí)行的的任務(wù)將會(huì)繼續(xù)執(zhí)行,但不會(huì)執(zhí)行新的任務(wù)。
void shutdown();
//嘗試停止所有主動(dòng)執(zhí)行的任務(wù),停止等待任務(wù)的處理,并返回正在等待執(zhí)行的任務(wù)列表。
List<Runnable> shutdownNow();
//如果這個(gè)Executor已被關(guān)閉,則返回 true 。
boolean isShutdown();
//如果所有任務(wù)在關(guān)閉后完成,則返回true 。 請(qǐng)注意, isTerminated從不true ,除非shutdown或shutdownNow先被執(zhí)行。
boolean isTerminated();
//阻止所有任務(wù)在關(guān)閉請(qǐng)求完成后執(zhí)行,或發(fā)生超時(shí),或當(dāng)前線(xiàn)程中斷,以先到者為準(zhǔn)。
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException;
//提交值返回任務(wù)以執(zhí)行,并返回代表任務(wù)待處理結(jié)果的Future。
<T> Future<T> submit(Callable<T> task);
//提交一個(gè)可運(yùn)行的任務(wù)執(zhí)行,并返回一個(gè)表示該任務(wù)的Future。Future的get方法將在成功完成后返回給定的結(jié)果。
<T> Future<T> submit(Runnable task, T result);
提交一個(gè)可運(yùn)行的任務(wù)執(zhí)行,并返回一個(gè)表示該任務(wù)的Future。Future的get方法將在成功完成后返回null。
Future<?> submit(Runnable task);
//執(zhí)行給定的任務(wù),返回持有他們的狀態(tài)和結(jié)果的所有完成的Future列表。
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException;
//執(zhí)行給定的任務(wù),返回在所有完成或超時(shí)到期時(shí)持有其狀態(tài)和結(jié)果的Future列表
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException;
//執(zhí)行給定的任務(wù),返回一個(gè)成功完成的結(jié)果(即沒(méi)有拋出異常),如果有的話(huà)。 正?;虍惓M顺龊螅形赐瓿傻娜蝿?wù)將被取消。
<T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException;
//執(zhí)行給定的任務(wù),返回一個(gè)已經(jīng)成功完成的結(jié)果(即,不拋出異常),如果有的話(huà)。
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
ScheduledExecutorService
ScheduledExecutorService接口繼承了ExecutorService接口,增加了定時(shí)任務(wù)相關(guān)的方法。
public interface ScheduledExecutorService extends ExecutorService
方法
/* 創(chuàng)建并執(zhí)行在給定延遲后啟用的單次操作。
參數(shù)
command - 要執(zhí)行的任務(wù)
delay - 從現(xiàn)在開(kāi)始延遲執(zhí)行的時(shí)間
unit - 延時(shí)參數(shù)的時(shí)間單位
結(jié)果
表示任務(wù)等待完成,并且它的ScheduledFuture get()方法將返回 null。
*/
public ScheduledFuture<?> schedule(Runnable command,
long delay, TimeUnit unit);
/* 創(chuàng)建并執(zhí)行在給定延遲后啟用的ScheduledFuture。
參數(shù)類(lèi)型
V - 可調(diào)用結(jié)果的類(lèi)型
參數(shù)
callable - 執(zhí)行的功能
delay - 從現(xiàn)在開(kāi)始延遲執(zhí)行的時(shí)間
unit - 延遲參數(shù)的時(shí)間單位
結(jié)果
一個(gè)可用于提取結(jié)果或取消的ScheduledFuture
*/
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
long delay, TimeUnit unit);
/*在初始延遲之后周期性的執(zhí)行command。
參數(shù)
command - 要執(zhí)行的任務(wù)
initialDelay - 延遲第一次執(zhí)行的時(shí)間
period - 連續(xù)執(zhí)行之間的時(shí)期
unit - initialDelay和period參數(shù)的時(shí)間單位
結(jié)果
一個(gè)ScheduledFuture代表待完成的任務(wù),其 get()方法將在取消時(shí)拋出異常
*/
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);
/**
創(chuàng)建并執(zhí)行在給定的初始延遲之后首先啟用的定期動(dòng)作,隨后在一個(gè)執(zhí)行的終止和下一個(gè)執(zhí)行的開(kāi)始之間給定的延遲。 如果任務(wù)的執(zhí)行遇到異常,則后續(xù)的執(zhí)行被抑制。 否則,任務(wù)將僅通過(guò)取消或終止執(zhí)行人終止。
參數(shù)
command - 要執(zhí)行的任務(wù)
initialDelay - 延遲第一次執(zhí)行的時(shí)間
delay - 一個(gè)執(zhí)行終止與下一個(gè)執(zhí)行的開(kāi)始之間的延遲
unit - initialDelay和delay參數(shù)的時(shí)間單位
結(jié)果
一個(gè)ScheduledFuture代表待完成的任務(wù),其 get()方法將在取消時(shí)拋出異常
*/
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
ThreadPoolExecutor
ThreadPoolExecutor是一個(gè)基礎(chǔ)、標(biāo)準(zhǔn)的線(xiàn)程池實(shí)現(xiàn)。
ThreadPoolExecutor中提供了四種構(gòu)造函數(shù)以創(chuàng)建線(xiàn)程池。
參數(shù)的定義如下:

public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
threadFactory, defaultHandler);
}
/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), handler);
}
/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
ScheduledThreadPoolExecutor
public class ScheduledThreadPoolExecutor
extends ThreadPoolExecutor
implements ScheduledExecutorService
繼承了ThreadPoolExecutor,實(shí)現(xiàn)了ScheduledExecutorService中定時(shí)任務(wù)相關(guān)的方法。


總結(jié)
到此這篇關(guān)于java線(xiàn)程池核心API源碼詳細(xì)分析的文章就介紹到這了,更多相關(guān)java中線(xiàn)程池內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java詳解多線(xiàn)程協(xié)作作業(yè)之信號(hào)同步
信號(hào)量同步是指在不同線(xiàn)程之間,通過(guò)傳遞同步信號(hào)量來(lái)協(xié)調(diào)線(xiàn)程執(zhí)行的先后次序。CountDownLatch是基于時(shí)間維度的Semaphore則是基于信號(hào)維度的2022-05-05
Java NIO 文件通道 FileChannel 用法及原理
這篇文章主要介紹了Java NIO 文件通道 FileChannel 用法和原理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
使用Springboot封裝一個(gè)自適配的數(shù)據(jù)單位轉(zhuǎn)換工具類(lèi)
我們?cè)诮邮涨芭_(tái)傳輸?shù)臄?shù)據(jù)時(shí),往往SpringBoot使用內(nèi)置的數(shù)據(jù)類(lèi)型轉(zhuǎn)換器把我們提交的數(shù)據(jù)自動(dòng)封裝成對(duì)象等類(lèi)型,下面這篇文章主要給大家介紹了關(guān)于使用Springboot封裝一個(gè)自適配的數(shù)據(jù)單位轉(zhuǎn)換工具類(lèi)的相關(guān)資料,需要的朋友可以參考下2023-03-03
Java實(shí)現(xiàn)醫(yī)院管理系統(tǒng)
這篇文章主要介為大家詳細(xì)紹了Java實(shí)現(xiàn)醫(yī)院管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Java詳細(xì)講解不同版本的接口語(yǔ)法和抽象類(lèi)與接口的區(qū)別
對(duì)于面向?qū)ο缶幊虂?lái)說(shuō),抽象是它的一大特征之一,在?Java?中可以通過(guò)兩種形式來(lái)體現(xiàn)OOP的抽象:接口和抽象類(lèi),下面這篇文章主要給大家介紹了關(guān)于Java入門(mén)基礎(chǔ)之抽象類(lèi)與接口的相關(guān)資料,需要的朋友可以參考下2022-04-04
win10系統(tǒng)64位jdk1.8的下載與安裝教程圖解
這篇文章主要介紹了win10系統(tǒng)64位jdk1.8的下載與安裝教程圖解,本文給大家介紹的非常詳細(xì),對(duì)大家的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
springBoot的事件機(jī)制GenericApplicationListener用法解析
這篇文章主要介紹了springBoot的事件機(jī)制GenericApplicationListener用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值的相關(guān)資料2019-09-09

