亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

jdk源代碼concurrent說明及底層執(zhí)行簡(jiǎn)單流程

sf190404 / 1690人閱讀

摘要:實(shí)現(xiàn)初始化線程池?cái)?shù)量策略并執(zhí)行線程。二簡(jiǎn)述下從定義線程池,到其底層執(zhí)行的簡(jiǎn)單流程簡(jiǎn)單定義了如上兩句代碼,自動(dòng)創(chuàng)建個(gè)固定大小的線程的線程池,實(shí)現(xiàn)了接口的對(duì)象以后,自動(dòng)啟動(dòng)線程并執(zhí)行對(duì)象的方法。關(guān)鍵的調(diào)用類方法,管理線程池并執(zhí)行任務(wù)。

一.類圖說明

Executor接口類,執(zhí)行Runnable接口execute。

ExecutorService接口類繼承Executor接口,包含提交執(zhí)行Runnable和Callable接口submit以及shutdown,invokeAll接口。

ScheduledExecutorService接口類繼承ExecutorService接口,主要包含計(jì)劃執(zhí)行接口schedule,scheduleAtFixedRate以及scheduleWithFixedDelay。

虛類AbstractExecutorService,實(shí)現(xiàn)ExecutorService接口。實(shí)現(xiàn)ExecutorService相關(guān)接口。

ThreadPoolExecutor類,繼承虛類AbstractExecutorService。實(shí)現(xiàn)初始化線程池?cái)?shù)量、策略并執(zhí)行線程。

ScheduledThreadPoolExecutor類,繼承ThreadPoolExecutor類并實(shí)現(xiàn)ScheduledExecutorService接口。實(shí)現(xiàn)計(jì)劃執(zhí)行相關(guān)接口。

執(zhí)行類,定義ThreadPoolExecutor和ScheduledThreadPoolExecutor類,并使用相關(guān)concurrent類方法。

二.簡(jiǎn)述下從定義線程池,到其底層執(zhí)行的簡(jiǎn)單流程

ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.submit(new NewTask());

簡(jiǎn)單定義了如上兩句代碼,JDK自動(dòng)創(chuàng)建3個(gè)固定大小的線程的線程池,submit實(shí)現(xiàn)了Runnable接口的NewTask對(duì)象以后,JDK自動(dòng)啟動(dòng)線程并執(zhí)行NewTask對(duì)象的run方法。流程是如何的呢?

1.Executors的newFixedThreadPool方法new了一個(gè)ThreadPoolExecutor對(duì)象,且new了一個(gè)LinkedBlockingQueue對(duì)象。

public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue());
    }
    

2.調(diào)用ThreadPoolExecutor的構(gòu)造函數(shù),其中ThreadFactory默認(rèn)使用defaultThreadFactory,defaultHandler為AbortPolicy。

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue 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.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }
    

3.初始化以后,調(diào)用AbstractExecutorService類的submit方法,執(zhí)行Runnable對(duì)象。

public Future submit(Runnable task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture ftask = newTaskFor(task, null);
        execute(ftask);
        return ftask;
    }
    

4.RunnableAdapter實(shí)現(xiàn)了Callable接口,F(xiàn)utureTask類包含callable類成員對(duì)象。
FutureTask實(shí)現(xiàn)RunnableFuture接口,RunnableFuture繼承Runnable接口。所以newTaskFor(task, null)方法返回一個(gè)FutureTask對(duì)象。

public class FutureTask implements RunnableFuture

public interface RunnableFuture extends Runnable, Future

RunnableFuture ftask = newTaskFor(task, null);

protected  RunnableFuture newTaskFor(Runnable runnable, T value) {
    return new FutureTask(runnable, value);
}

public FutureTask(Runnable runnable, V result) {
    this.callable = Executors.callable(runnable, result);
    this.state = NEW;       // ensure visibility of callable
}

public static  Callable callable(Runnable task, T result) {
    if (task == null)
        throw new NullPointerException();
    return new RunnableAdapter(task, result);
}

static final class RunnableAdapter implements Callable {
    final Runnable task;
    final T result;
    RunnableAdapter(Runnable task, T result) {
        this.task = task;
        this.result = result;
    }
    public T call() {
        task.run();
        return result;
    }
}

5.關(guān)鍵的調(diào)用ThreadPoolExecutor類execute方法,管理線程池并執(zhí)行Runnable任務(wù)。主要邏輯如下:
1)如果線程數(shù)量小于corePoolSize,新來一個(gè)任務(wù),創(chuàng)建一個(gè)新線程。
2)如果線程數(shù)量等于corePoolSize,則將任務(wù)緩存到workQueue中。
3)如果線程數(shù)量等于corePoolSize并且workQueue隊(duì)列已滿,則繼續(xù)創(chuàng)建線程直到等于maximumPoolSize。
4)如果線程數(shù)量等于maximumPoolSize,且workQueue隊(duì)列已滿,則根據(jù)defaultHandler策略執(zhí)行相應(yīng)措施。默認(rèn)是AbortPolicy,拋出一個(gè)運(yùn)行時(shí)異常RejectedExecutionException。另外還有3種策略:CallerRunsPolicy->如果線程池沒有shutdown,則直接調(diào)用Runnable的run方法執(zhí)行。如果線程池shutdown,則直接丟棄;DiscardPolicy->直接丟棄,沒有任何異常;DiscardOldestPolicy->丟棄最老的任務(wù),并調(diào)用線程池的execute方法執(zhí)行,如果線程池沒有shutdown。

public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }
    
       

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/69881.html

相關(guān)文章

  • Java多線程進(jìn)階(一)—— J.U.C并發(fā)包概述

    摘要:整個(gè)包,按照功能可以大致劃分如下鎖框架原子類框架同步器框架集合框架執(zhí)行器框架本系列將按上述順序分析,分析所基于的源碼為。后,根據(jù)一系列常見的多線程設(shè)計(jì)模式,設(shè)計(jì)了并發(fā)包,其中包下提供了一系列基礎(chǔ)的鎖工具,用以對(duì)等進(jìn)行補(bǔ)充增強(qiáng)。 showImg(https://segmentfault.com/img/remote/1460000016012623); 本文首發(fā)于一世流云專欄:https...

    anonymoussf 評(píng)論0 收藏0
  • 一文了解JVM全部垃圾回收器,從Serial到ZGC

    摘要:是目前的實(shí)驗(yàn)收集器。也是需要暫停程序一切的工作,然后多線程執(zhí)行垃圾回收。與最大的不同,它關(guān)注的是垃圾回收的吞吐量。這里的吞吐量指的是總時(shí)間與垃圾回收時(shí)間的比例。篩選回收,評(píng)估標(biāo)記垃圾,根據(jù)模式回收垃圾。 《對(duì)象搜索算法與回收算法》介紹了垃圾回收的基礎(chǔ)算法,相當(dāng)于垃圾回收的方法論。接下來就詳細(xì)看看垃圾回收的具體實(shí)現(xiàn)。 上文提到過現(xiàn)代的商用虛擬機(jī)的都是采用分代收集的,不同的區(qū)域用不同的收集...

    jasperyang 評(píng)論0 收藏0
  • Java線程池簡(jiǎn)單總結(jié)

    摘要:本文主要內(nèi)容為簡(jiǎn)單總結(jié)中線程池的相關(guān)信息。方法簇方法簇用于創(chuàng)建固定線程數(shù)的線程池。三種常見線程池的對(duì)比上文總結(jié)了工具類創(chuàng)建常見線程池的方法,現(xiàn)對(duì)三種線程池區(qū)別進(jìn)行比較。 概述 線程可認(rèn)為是操作系統(tǒng)可調(diào)度的最小的程序執(zhí)行序列,一般作為進(jìn)程的組成部分,同一進(jìn)程中多個(gè)線程可共享該進(jìn)程的資源(如內(nèi)存等)。在單核處理器架構(gòu)下,操作系統(tǒng)一般使用分時(shí)的方式實(shí)現(xiàn)多線程;在多核處理器架構(gòu)下,多個(gè)線程能夠...

    CoorChice 評(píng)論0 收藏0
  • 為Java程序員金三銀四精心挑選的300余道Java面試題與答案

    摘要:為程序員金三銀四精心挑選的余道面試題與答案,歡迎大家向我推薦你在面試過程中遇到的問題我會(huì)把大家推薦的問題添加到下面的常用面試題清單中供大家參考。 為Java程序員金三銀四精心挑選的300余道Java面試題與答案,歡迎大家向我推薦你在面試過程中遇到的問題,我會(huì)把大家推薦的問題添加到下面的常用面試題清單中供大家參考。 前兩天寫的以下博客,大家比較認(rèn)可,熱度不錯(cuò),希望可以幫到準(zhǔn)備或者正在參加...

    tomorrowwu 評(píng)論0 收藏0
  • 想進(jìn)大廠?50個(gè)多線程面試題,你會(huì)多少?(一)

    摘要:下面是線程相關(guān)的熱門面試題,你可以用它來好好準(zhǔn)備面試。線程安全問題都是由全局變量及靜態(tài)變量引起的。持有自旋鎖的線程在之前應(yīng)該釋放自旋鎖以便其它線程可以獲得自旋鎖。 最近看到網(wǎng)上流傳著,各種面試經(jīng)驗(yàn)及面試題,往往都是一大堆技術(shù)題目貼上去,而沒有答案。 不管你是新程序員還是老手,你一定在面試中遇到過有關(guān)線程的問題。Java語言一個(gè)重要的特點(diǎn)就是內(nèi)置了對(duì)并發(fā)的支持,讓Java大受企業(yè)和程序員...

    wow_worktile 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

sf190404

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<