最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java線程池核心API源碼詳細分析

 更新時間:2022年01月23日 10:50:42   作者:1?+?1=王  
大家好,本篇文章主要講的是java線程池核心API源碼詳細分析,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽

概述

線程池是一種多線程處理形式,處理過程中將任務添加到隊列,然后在創(chuàng)建線程后自動啟動這些任務。線程池線程都是后臺線程。每個線程都使用默認的堆棧大小,以默認的優(yōu)先級運行,并處于多線程單元中。如果某個線程在托管代碼中空閑(如正在等待某個事件),則線程池將插入另一個輔助線程來使所有處理器保持繁忙。如果所有線程池線程都始終保持繁忙,但隊列中包含掛起的工作,則線程池將在一段時間后創(chuàng)建另一個輔助線程但線程的數(shù)目永遠不會超過最大值。超過最大值的線程可以排隊,但他們要等到其他線程完成后才啟動。

jdk中當然也提供了一些操作線程池的API。

java中操作線程池的api位于java.util.concurrent;包中。

常見的核心接口和實現(xiàn)類包括Executor接口、ExecutorService接口、ScheduledExecutorService接口、ThreadPoolExecutor實現(xiàn)類、ScheduledThreadPoolExecutor實現(xiàn)類等。

Executor接口
最上層的接口,定義了執(zhí)行任務的execute()方法。

在這里插入圖片描述

ExecutorService接口
繼承了Executor接口,擴展了Callable、Future、關閉方法。

在這里插入圖片描述

ScheduledExecutorService接口
繼承了ExecutorService接口,增加了定時任務相關的方法。

在這里插入圖片描述

ThreadPoolExecutor實現(xiàn)類
基礎、標準的線程池實現(xiàn)。

ScheduledThreadPoolExecutor實現(xiàn)類
繼承了ThreadPoolExecutor,實現(xiàn)了ScheduledExecutorService中定時任務相關的方法。

他們之間的繼承實現(xiàn)關系圖如下:

在這里插入圖片描述

源碼分析

下面對Executor接口、ExecutorService接口、ScheduledExecutorService接口、ThreadPoolExecutor實現(xiàn)類、ScheduledThreadPoolExecutor實現(xiàn)類這幾個核心api的源碼進行分析。

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是最上層的接口,該接口提供了一個execute()方法,用于執(zhí)行指定的Runnable任務。

ExecutorService

ExecutorService接口繼承至Executor接口,擴展了Callable、Future、關閉等方法。

public interface ExecutorService extends Executor

定義的方法

     //啟動有序關閉,先前已經(jīng)執(zhí)行的的任務將會繼續(xù)執(zhí)行,但不會執(zhí)行新的任務。
    void shutdown();

     //嘗試停止所有主動執(zhí)行的任務,停止等待任務的處理,并返回正在等待執(zhí)行的任務列表。
    List<Runnable> shutdownNow();

     //如果這個Executor已被關閉,則返回 true 。 
    boolean isShutdown();

    //如果所有任務在關閉后完成,則返回true 。 請注意, isTerminated從不true ,除非shutdown或shutdownNow先被執(zhí)行。 
    boolean isTerminated();

    //阻止所有任務在關閉請求完成后執(zhí)行,或發(fā)生超時,或當前線程中斷,以先到者為準。 
    boolean awaitTermination(long timeout, TimeUnit unit)
        throws InterruptedException;

    //提交值返回任務以執(zhí)行,并返回代表任務待處理結果的Future。
    <T> Future<T> submit(Callable<T> task);

    //提交一個可運行的任務執(zhí)行,并返回一個表示該任務的Future。Future的get方法將在成功完成后返回給定的結果。
    <T> Future<T> submit(Runnable task, T result);

    提交一個可運行的任務執(zhí)行,并返回一個表示該任務的Future。Future的get方法將在成功完成后返回null。
    Future<?> submit(Runnable task);

    //執(zhí)行給定的任務,返回持有他們的狀態(tài)和結果的所有完成的Future列表。
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
        throws InterruptedException;

    //執(zhí)行給定的任務,返回在所有完成或超時到期時持有其狀態(tài)和結果的Future列表
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                                  long timeout, TimeUnit unit)
        throws InterruptedException;

    //執(zhí)行給定的任務,返回一個成功完成的結果(即沒有拋出異常),如果有的話。 正常或異常退出后,尚未完成的任務將被取消。
    <T> T invokeAny(Collection<? extends Callable<T>> tasks)
        throws InterruptedException, ExecutionException;

    //執(zhí)行給定的任務,返回一個已經(jīng)成功完成的結果(即,不拋出異常),如果有的話。
    <T> T invokeAny(Collection<? extends Callable<T>> tasks,
                    long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;

ScheduledExecutorService

ScheduledExecutorService接口繼承了ExecutorService接口,增加了定時任務相關的方法。

public interface ScheduledExecutorService extends ExecutorService

方法

     /* 創(chuàng)建并執(zhí)行在給定延遲后啟用的單次操作。

	參數(shù)
	    command - 要執(zhí)行的任務 
	    delay - 從現(xiàn)在開始延遲執(zhí)行的時間 
	    unit - 延時參數(shù)的時間單位 
	結果
	    表示任務等待完成,并且它的ScheduledFuture get()方法將返回 null。
    */
    public ScheduledFuture<?> schedule(Runnable command,
                                       long delay, TimeUnit unit);

    /* 創(chuàng)建并執(zhí)行在給定延遲后啟用的ScheduledFuture。
		參數(shù)類型
		    V - 可調用結果的類型 
		參數(shù)
		    callable - 執(zhí)行的功能 
		    delay - 從現(xiàn)在開始延遲執(zhí)行的時間 
		    unit - 延遲參數(shù)的時間單位 
		結果
		    一個可用于提取結果或取消的ScheduledFuture 
     */
    public <V> ScheduledFuture<V> schedule(Callable<V> callable,
                                           long delay, TimeUnit unit);

    /*在初始延遲之后周期性的執(zhí)行command。
     參數(shù)
	    command - 要執(zhí)行的任務 
	    initialDelay - 延遲第一次執(zhí)行的時間 
	    period - 連續(xù)執(zhí)行之間的時期 
	    unit - initialDelay和period參數(shù)的時間單位 
	結果
	    一個ScheduledFuture代表待完成的任務,其 get()方法將在取消時拋出異常 
    */
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                                  long initialDelay,
                                                  long period,
                                                  TimeUnit unit);

    /**
      創(chuàng)建并執(zhí)行在給定的初始延遲之后首先啟用的定期動作,隨后在一個執(zhí)行的終止和下一個執(zhí)行的開始之間給定的延遲。 如果任務的執(zhí)行遇到異常,則后續(xù)的執(zhí)行被抑制。 否則,任務將僅通過取消或終止執(zhí)行人終止。
		參數(shù)
		    command - 要執(zhí)行的任務 
		    initialDelay - 延遲第一次執(zhí)行的時間 
		    delay - 一個執(zhí)行終止與下一個執(zhí)行的開始之間的延遲 
		    unit - initialDelay和delay參數(shù)的時間單位 
		結果
		    一個ScheduledFuture代表待完成的任務,其 get()方法將在取消時拋出異常 
     */
    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit);

ThreadPoolExecutor

ThreadPoolExecutor是一個基礎、標準的線程池實現(xiàn)。

ThreadPoolExecutor中提供了四種構造函數(shù)以創(chuàng)建線程池。

參數(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,實現(xiàn)了ScheduledExecutorService中定時任務相關的方法。

在這里插入圖片描述

在這里插入圖片描述

總結

到此這篇關于java線程池核心API源碼詳細分析的文章就介紹到這了,更多相關java中線程池內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java TreeSet類的簡單理解和使用

    Java TreeSet類的簡單理解和使用

    這篇文章主要介紹了Java TreeSet類的簡單理解和使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • Springboot自動加載配置的原理解析

    Springboot自動加載配置的原理解析

    Springboot遵循“約定優(yōu)于配置”的原則,使用注解對一些常規(guī)的配置項做默認配置,減少或不使用xml配置,讓你的項目快速運行起來,這篇文章主要給大家介紹了關于Springboot自動加載配置原理的相關資料,需要的朋友可以參考下
    2021-10-10
  • springboot實現(xiàn)單文件和多文件上傳

    springboot實現(xiàn)單文件和多文件上傳

    這篇文章主要為大家詳細介紹了springboot實現(xiàn)單文件和多文件上傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Java調整圖片大小的3種方式小結

    Java調整圖片大小的3種方式小結

    在軟件開發(fā)中處理圖像是一個常見任務,特別是當我們需要優(yōu)化圖像尺寸以適應不同的應用場景時,這篇文章主要介紹了Java調整圖片大小的3種方式,需要的朋友可以參考下
    2024-09-09
  • Springboot如何集成websocket

    Springboot如何集成websocket

    這篇文章主要介紹了Springboot如何集成websocket問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 使用@RequestParam設置默認可以傳空值

    使用@RequestParam設置默認可以傳空值

    這篇文章主要介紹了使用@RequestParam設置默認可以傳空值的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java反射之深入理解

    Java反射之深入理解

    這篇文章主要介紹了Java反射機制的深入理解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Java如何自定義線程池中隊列

    Java如何自定義線程池中隊列

    這篇文章主要介紹了Java如何自定義線程池中隊列,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • Opencv實現(xiàn)身份證OCR識別的示例詳解

    Opencv實現(xiàn)身份證OCR識別的示例詳解

    這篇文章主要為大家詳細介紹了如何使用Opencv實現(xiàn)身份證OCR識別功能,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以跟隨小編一起了解一下
    2024-03-03
  • 詳解JAVA 內存管理

    詳解JAVA 內存管理

    這篇文章主要介紹了JAVA 內存管理的相關資料,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07

最新評論

饶阳县| 甘谷县| 抚顺市| 沂南县| 介休市| 许昌县| 葵青区| 巴马| 漯河市| 鄢陵县| 镇平县| 青州市| 册亨县| 大英县| 会泽县| 石嘴山市| 崇文区| 五寨县| 晴隆县| 民权县| 鹤岗市| 旬邑县| 宁陵县| 盐池县| 临城县| 长岛县| 壶关县| 确山县| 天峻县| 论坛| 南投市| 兴隆县| 武清区| 偏关县| 大宁县| 临汾市| 海口市| 会同县| 利川市| 华安县| 威宁|