Android線程篇(四):深入理解Java線程池(二)

上面文章我們發(fā)現(xiàn),ThreadPoolExecutor 繼承自AbstractExecutorService,這篇文章我們就來(lái)扒光它的衣服……
來(lái)來(lái)來(lái),先看看AbstractExecutorService源碼:

public abstract class AbstractExecutorService implements ExecutorService 

發(fā)現(xiàn):AbstractExecutorService實(shí)現(xiàn)了ExecutorService接口,來(lái)看看ExecutorService源碼:

public interface ExecutorService extends Executor {
    void shutdown();
    List<Runnable> shutdownNow();
    boolean isShutdown();
    boolean isTerminated();
    boolean awaitTermination(long timeout, TimeUnit unit)
        throws InterruptedException;
    <T> Future<T> submit(Callable<T> task);
    <T> Future<T> submit(Runnable task, T result);
    Future<?> submit(Runnable task);
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
        throws InterruptedException;
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                                  long timeout, TimeUnit unit)
        throws InterruptedException;
    <T> T invokeAny(Collection<? extends Callable<T>> tasks)
        throws InterruptedException, ExecutionException;
    <T> T invokeAny(Collection<? extends Callable<T>> tasks,
                    long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
}

ExecutorService又是繼承了Executor接口,我們看一下Executor接口的實(shí)現(xiàn):

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);
}

哈哈,衣服扒了一半了,到這里我們得出結(jié)論,ThreadPoolExecutor繼承自AbstractExecutorService,AbstractExecutorService 又實(shí)現(xiàn)了ExecutorService接口,ExecutorService接口又繼承了Executor接口。

Executor接口里面只聲明了一個(gè)方法execute(Runnable),返回值為void,參數(shù)為Runnable類型,從字面意思可以理解,就是用來(lái)執(zhí)行傳進(jìn)去的任務(wù)的;

然后ExecutorService接口繼承自Executor接口,并且聲明了一些方法:submit提交、invokeAll調(diào)用所有、invokeAny調(diào)用任何一個(gè)以及shutDown關(guān)閉線程池等等等等;

抽象類AbstractExecutorService實(shí)現(xiàn)了ExecutorService接口,實(shí)現(xiàn)了ExecutorService中聲明的方法;

ThreadPoolExecutor繼承了AbstractExecutorService

介紹下ThreadPoolExecutor中幾個(gè)重要的方法:

  • execute():execute()方法實(shí)際上是Executor接口中聲明的方法,在ThreadPoolExecutor進(jìn)行了具體的實(shí)現(xiàn),這個(gè)方法是ThreadPoolExecutor的核心方法,通過這個(gè)方法可以向線程池提交一個(gè)任務(wù),交由線程池去執(zhí)行。

  • submit():submit()方法是在ExecutorService中聲明的方法,在AbstractExecutorService就已經(jīng)有了具體的實(shí)現(xiàn),在ThreadPoolExecutor中并沒有對(duì)其進(jìn)行重寫,這個(gè)方法也是用來(lái)向線程池提交任務(wù)的,但是它和execute()方法不同,它能夠返回任務(wù)執(zhí)行的結(jié)果,去看submit()方法的實(shí)現(xiàn),會(huì)發(fā)現(xiàn)它實(shí)際上還是調(diào)用的execute()方法,只不過它利用了Future來(lái)獲取任務(wù)執(zhí)行結(jié)果。

  • shutdown():當(dāng)線程池調(diào)用該方法時(shí),線程池的狀態(tài)則立刻變成SHUTDOWN狀態(tài)。此時(shí),則不能再往線程池中添加任何任務(wù),否則將會(huì)拋出RejectedExecutionException異常。但是,此時(shí)線程池不會(huì)立刻退出,直到添加到線程池中的任務(wù)都已經(jīng)處理完成,才會(huì)退出。

  • shutdownNow():執(zhí)行該方法,線程池的狀態(tài)立刻變成STOP狀態(tài),并試圖停止所有正在執(zhí)行的線程,不再處理還在池隊(duì)列中等待的任務(wù),當(dāng)然,它會(huì)返回那些未執(zhí)行的任務(wù)。它試圖終止線程的方法是通過調(diào)用Thread.interrupt()方法來(lái)實(shí)現(xiàn)的,但是大家知道,這種方法的作用有限,如果線程中沒有sleep 、wait、Condition、定時(shí)鎖等應(yīng)用, interrupt()方法是無(wú)法中斷當(dāng)前的線程的。所以,shutdownNow()并不代表線程池就一定立即就能退出,它可能必須要等待所有正在執(zhí)行的任務(wù)都執(zhí)行完成了才能退出。

還有其他方法:getQueue()、getPoolSize()、getActiveCount()、getCompletedTaskCount()等獲取與線程池相關(guān)屬性的方法,有興趣的朋友可以自行查看源碼。

到這里AbstractExecutorService的衣服已經(jīng)扒完了,我感覺到了個(gè)無(wú)底洞,知識(shí)點(diǎn)太多了?。?!博主水平有限,好些東西也是一知半解,如有不當(dāng)之處還望大家指正……

寫博客真的是一件耗費(fèi)精力的事情,大家且行且珍惜,下篇講什么呢?讓博主回去好好思考一下……

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容