成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

java并發(fā)編程學習之線程池-AbstractExecutorService(二)

Jokcy / 633人閱讀

摘要:抽象類,實現(xiàn)了的接口。將任務(wù)封裝成提交任務(wù)主要方法在任務(wù)是否超時超時時間任務(wù)書用于存放結(jié)果的,先完成的放前面。

AbstractExecutorService抽象類,實現(xiàn)了ExecutorService的接口。

newTaskFor

將任務(wù)封裝成FutureTask

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

提交任務(wù)

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

主要方法在doInvokeAny

//tasks任務(wù)
//timed是否超時
//nanos超時時間
private  T doInvokeAny(Collection> tasks,
                              boolean timed, long nanos)
    throws InterruptedException, ExecutionException, TimeoutException {
    if (tasks == null)
        throw new NullPointerException();
    int ntasks = tasks.size();//任務(wù)書
    if (ntasks == 0)
        throw new IllegalArgumentException();
    ArrayList> futures = new ArrayList>(ntasks);
    //用于存放結(jié)果的,先完成的放前面。所以第一個任務(wù)沒完成的時候,會繼續(xù)提交后續(xù)任務(wù)
    ExecutorCompletionService ecs =
        new ExecutorCompletionService(this);

    try {     
        //異常信息
        ExecutionException ee = null;
        //過期時間
        final long deadline = timed ? System.nanoTime() + nanos : 0L;
        Iterator> it = tasks.iterator();//獲取第一個任務(wù)
        提交任務(wù)
        futures.add(ecs.submit(it.next()));
        --ntasks;//因為提交了一個,任務(wù)數(shù)-1
        int active = 1;//正在執(zhí)行的任務(wù)

        for (;;) {
            Future f = ecs.poll();
            if (f == null) {//第一個沒完成
                if (ntasks > 0) {//還有沒提交的任務(wù)
                    --ntasks;//任務(wù)數(shù)-1
                    futures.add(ecs.submit(it.next()));//提交任務(wù)
                    ++active;//正在執(zhí)行的任務(wù)+1
                }
                else if (active == 0)//當前沒任務(wù)了,但是都失敗了,異常被捕獲了
                    break;
                else if (timed) {
                    f = ecs.poll(nanos, TimeUnit.NANOSECONDS);//等待
                    if (f == null)//返回空,超時拋出異常,結(jié)束
                        throw new TimeoutException();
                    nanos = deadline - System.nanoTime();//剩余時間
                }
                else
                    f = ecs.take();//阻塞等待獲取
            }
            if (f != null) {//說明已經(jīng)執(zhí)行完
                --active;//任務(wù)數(shù)-1
                try {
                    return f.get();//返回執(zhí)行結(jié)果
                } catch (ExecutionException eex) {
                    ee = eex;
                } catch (RuntimeException rex) {
                    ee = new ExecutionException(rex);
                }
            }
        }

        if (ee == null)
            ee = new ExecutionException();
        throw ee;

    } finally {
        //取消其他任務(wù),畢竟第一個結(jié)果已經(jīng)返回了
        for (int i = 0, size = futures.size(); i < size; i++)
            futures.get(i).cancel(true);
    }
}

public  T invokeAny(Collection> tasks)
    throws InterruptedException, ExecutionException {
    try {
        return doInvokeAny(tasks, false, 0);
    } catch (TimeoutException cannotHappen) {
        assert false;
        return null;
    }
}

public  T invokeAny(Collection> tasks,
                       long timeout, TimeUnit unit)
    throws InterruptedException, ExecutionException, TimeoutException {
    return doInvokeAny(tasks, true, unit.toNanos(timeout));
}
invokeAll

返回所有任務(wù)的結(jié)果

public  List> invokeAll(Collection> tasks)
    throws InterruptedException {
    if (tasks == null)
        throw new NullPointerException();
    ArrayList> futures = new ArrayList>(tasks.size());//
    boolean done = false;
    try {
        for (Callable t : tasks) {//封裝任務(wù),并提交
            RunnableFuture f = newTaskFor(t);
            futures.add(f);
            execute(f);
        }
        for (int i = 0, size = futures.size(); i < size; i++) {
            Future f = futures.get(i);
            if (!f.isDone()) {
                try {
                    f.get();//阻塞,等待結(jié)果
                } catch (CancellationException ignore) {
                } catch (ExecutionException ignore) {
                }
            }
        }
        done = true;
        return futures;
    } finally {
        if (!done)//有異常,取消
            for (int i = 0, size = futures.size(); i < size; i++)
                futures.get(i).cancel(true);
    }
}

public  List> invokeAll(Collection> tasks,
                                     long timeout, TimeUnit unit)
    throws InterruptedException {
    if (tasks == null)
        throw new NullPointerException();
    long nanos = unit.toNanos(timeout);
    ArrayList> futures = new ArrayList>(tasks.size());
    boolean done = false;
    try {
        for (Callable t : tasks)
            futures.add(newTaskFor(t));

        final long deadline = System.nanoTime() + nanos;
        final int size = futures.size();

        // Interleave time checks and calls to execute in case
        // executor doesn"t have any/much parallelism.
        
        for (int i = 0; i < size; i++) {
            execute((Runnable)futures.get(i));
            nanos = deadline - System.nanoTime();
            if (nanos <= 0L)
                return futures;//每個提交都要判斷,超時了返回Future
        }

        for (int i = 0; i < size; i++) {
            Future f = futures.get(i);
            if (!f.isDone()) {
                if (nanos <= 0L)
                    return futures;
                try {
                    f.get(nanos, TimeUnit.NANOSECONDS);
                } catch (CancellationException ignore) {
                } catch (ExecutionException ignore) {
                } catch (TimeoutException toe) {
                    return futures;
                }
                nanos = deadline - System.nanoTime();
            }
        }
        done = true;
        return futures;
    } finally {
        if (!done)
            for (int i = 0, size = futures.size(); i < size; i++)
                futures.get(i).cancel(true);
    }
}

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

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/75708.html

相關(guān)文章

  • java并發(fā)編程習之線程-預(yù)定義線程(四)

    摘要:系統(tǒng)預(yù)定了幾個線程池,不過建議手動創(chuàng)建,以防止錯誤創(chuàng)建消耗資源,比如創(chuàng)建太多線程或者固定線程數(shù)量,無界隊列固定線程數(shù)量,數(shù)量為,無界隊列,會按順序執(zhí)行不限制線程數(shù)量,使用隊列,使用于短任務(wù)基于用于周期性執(zhí)行任務(wù)示例第一個是,第二個是第一 系統(tǒng)預(yù)定了幾個線程池,不過建議手動創(chuàng)建,以防止錯誤創(chuàng)建消耗資源,比如創(chuàng)建太多線程或者OOM FixedThreadPool 固定線程數(shù)量,無界隊列 p...

    suemi 評論0 收藏0
  • java并發(fā)編程習之線程-ThreadPoolExecutor(三)

    摘要:是所有線程池實現(xiàn)的父類,我們先看看構(gòu)造函數(shù)構(gòu)造參數(shù)線程核心數(shù)最大線程數(shù)線程空閑后,存活的時間,只有線程數(shù)大于的時候生效存活時間的單位任務(wù)的阻塞隊列創(chuàng)建線程的工程,給線程起名字當線程池滿了,選擇新加入的任務(wù)應(yīng)該使用什么策略,比如拋異常丟棄當前 ThreadPoolExecutor ThreadPoolExecutor是所有線程池實現(xiàn)的父類,我們先看看構(gòu)造函數(shù) 構(gòu)造參數(shù) corePool...

    阿羅 評論0 收藏0
  • java并發(fā)編程習之線程-Executor和ExecutorService(一)

    摘要:接口用于提交任務(wù)接口繼承了接口設(shè)置線程的狀態(tài),還沒執(zhí)行的線程會被中斷設(shè)置線程的狀態(tài),嘗試停止正在進行的線程當調(diào)用或方法后返回為當調(diào)用方法后,并且所有提交的任務(wù)完成后返回為當調(diào)用方法后,成功停止后返回為當前線程阻塞,直到線程執(zhí)行完時間到被中斷 Executor接口 void execute(Runnable command)//用于提交command任務(wù) ExecutorService接...

    liuchengxu 評論0 收藏0
  • java并發(fā)編程習之線程的生命周期-start(

    摘要:與執(zhí)行方法,是用來啟動線程的,此時線程處于就緒狀態(tài),獲得調(diào)度后運行方法。執(zhí)行方法,相對于普通方法調(diào)用,在主線程調(diào)用。程序是順序執(zhí)行的,執(zhí)行完才會執(zhí)行下面的程序。 start與run 執(zhí)行start方法,是用來啟動線程的,此時線程處于就緒狀態(tài),獲得調(diào)度后運行run方法。run方法執(zhí)行結(jié)束,線程就結(jié)束。 執(zhí)行run方法,相對于普通方法調(diào)用,在主線程調(diào)用。程序是順序執(zhí)行的,執(zhí)行完才會執(zhí)行下...

    bigdevil_s 評論0 收藏0
  • Java線程習(八)線程與Executor 框架

    摘要:一使用線程池的好處線程池提供了一種限制和管理資源包括執(zhí)行一個任務(wù)。每個線程池還維護一些基本統(tǒng)計信息,例如已完成任務(wù)的數(shù)量。通過重復利用已創(chuàng)建的線程降低線程創(chuàng)建和銷毀造成的消耗。使用無界隊列作為線程池的工作隊列會對線程池帶來的影響與相同。 歷史優(yōu)質(zhì)文章推薦: Java并發(fā)編程指南專欄 分布式系統(tǒng)的經(jīng)典基礎(chǔ)理論 可能是最漂亮的Spring事務(wù)管理詳解 面試中關(guān)于Java虛擬機(jvm)的問...

    cheng10 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<