Android線程池總結(jié)筆記

參考鏈接:https://blog.csdn.net/u012702547/article/details/52259529

場(chǎng)景:listview中的item加載圖片,通過(guò)new Thread開(kāi)啟線程請(qǐng)求網(wǎng)絡(luò)

使用new Thread()創(chuàng)建線程存在的問(wèn)題

  • 針對(duì)每一個(gè)item都創(chuàng)建一個(gè)新線程,這樣會(huì)導(dǎo)致頻繁的創(chuàng)建線程,線程執(zhí)行完之后又被回收,又會(huì)導(dǎo)致頻繁的GC
  • 這么多線程缺乏統(tǒng)一管理,各線程之間互相競(jìng)爭(zhēng),降低程序的運(yùn)行效率,手機(jī)頁(yè)面卡頓,甚至?xí)?dǎo)致程序崩潰
  • 如果一個(gè)item滑出頁(yè)面,則要停止該item上圖片的加載,但是如果使用這種方式來(lái)創(chuàng)建線程,則無(wú)法實(shí)現(xiàn)線程停止執(zhí)行

使用線程池的好處

  • 重用已經(jīng)創(chuàng)建的好的線程,避免頻繁創(chuàng)建進(jìn)而導(dǎo)致的頻繁GC
  • 控制線程并發(fā)數(shù),合理使用系統(tǒng)資源,提高應(yīng)用性能
  • 可以有效的控制線程的執(zhí)行,比如定時(shí)執(zhí)行,取消執(zhí)行等

Executor本身是一個(gè)接口

ThreadPoolExecutor Executor的實(shí)現(xiàn)類

/**
      /**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @param maximumPoolSize the maximum number of threads to allow in the
     *        pool
     * @param keepAliveTime when the number of threads is greater than
     *        the core, this is the maximum time that excess idle threads
     *        will wait for new tasks before terminating.
     * @param unit the time unit for the {@code keepAliveTime} argument
     * @param workQueue the queue to use for holding tasks before they are
     *        executed.  This queue will hold only the {@code Runnable}
     *        tasks submitted by the {@code execute} method.
     * @param threadFactory the factory to use when the executor
     *        creates a new thread
     * @param handler the handler to use when execution is blocked
     *        because the thread bounds and queue capacities are reached
     * @throws IllegalArgumentException if one of the following holds:<br>
     *         {@code corePoolSize < 0}<br>
     *         {@code keepAliveTime < 0}<br>
     *         {@code maximumPoolSize <= 0}<br>
     *         {@code maximumPoolSize < corePoolSize}
     * @throws NullPointerException if {@code workQueue}
     *         or {@code threadFactory} or {@code handler} is null
     */
    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.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

ThreadPoolExecutor構(gòu)造方法參數(shù)的解釋

corePoolSize

核心線程,即在線程池中保留的線程數(shù),即使它們處于空閑狀態(tài)也保留,除非設(shè)置了{(lán)@code allowCoreThreadTimeOut}

maximumPoolSize:

線程池中允許的最大線程數(shù)

keepAliveTime

非核心線程的超時(shí)時(shí)長(zhǎng),當(dāng)系統(tǒng)中非核心線程閑置時(shí)間超過(guò)keepAliveTime之后,則會(huì)被回收。如果ThreadPoolExecutor的allowCoreThreadTimeOut屬性設(shè)置為true,則該參數(shù)也表示核心線程的超時(shí)時(shí)長(zhǎng)

unit

參數(shù)的時(shí)間單位,有納秒、微秒、毫秒、秒、分、時(shí)、天等

workQueue

線程池中的任務(wù)隊(duì)列,該隊(duì)列主要用來(lái)存儲(chǔ)已經(jīng)被提交但是尚未執(zhí)行的任務(wù)。存儲(chǔ)在這里的任務(wù)是由ThreadPoolExecutor的execute方法提交來(lái)的。
workQueue是一個(gè)BlockingQueue類型,是一個(gè)特殊的隊(duì)列,當(dāng)我們從BlockingQueue中取數(shù)據(jù)時(shí),如果BlockingQueue是空的,則取數(shù)據(jù)的操作會(huì)進(jìn)入到阻塞狀態(tài),當(dāng)BlockingQueue中有了新數(shù)據(jù)時(shí),這個(gè)取數(shù)據(jù)的操作又會(huì)被重新喚醒。同理,如果BlockingQueue中的數(shù)據(jù)已經(jīng)滿了,往BlockingQueue中存數(shù)據(jù)的操作又會(huì)進(jìn)入阻塞狀態(tài),直到BlockingQueue中又有新的空間,存數(shù)據(jù)的操作又會(huì)被沖洗喚醒。

threadFactory

在執(zhí)行時(shí)使用,為線程池提供創(chuàng)建新線程的功能,這個(gè)我們一般使用默認(rèn)即可

handler

拒絕策略,當(dāng)線程無(wú)法執(zhí)行新任務(wù)時(shí)(一般是由于線程池中的線程數(shù)量已經(jīng)達(dá)到最大數(shù)或者線程池關(guān)閉導(dǎo)致的),默認(rèn)情況下,當(dāng)線程池?zé)o法處理新線程時(shí),會(huì)拋出一個(gè)RejectedExecutionException。

線程提交到線程池之后運(yùn)行規(guī)則,遵循如下規(guī)則:

  • 1.execute一個(gè)線程之后,如果線程池中的線程數(shù)未達(dá)到核心線程數(shù),則會(huì)立馬啟用一個(gè)核心線程去執(zhí)行
  • 2.execute一個(gè)線程之后,如果線程池中的線程數(shù)已經(jīng)達(dá)到核心線程數(shù),且workQueue未滿,則將新線程放入workQueue中等待執(zhí)行
  • 3.execute一個(gè)線程之后,如果線程池中的線程數(shù)已經(jīng)達(dá)到核心線程數(shù)但未超過(guò)非核心線程數(shù),且workQueue已滿,則開(kāi)啟一個(gè)非核心線程來(lái)執(zhí)行任務(wù)
  • 4.execute一個(gè)線程之后,如果線程池中的線程數(shù)已經(jīng)超過(guò)非核心線程數(shù),則拒絕執(zhí)行該任務(wù)
最后編輯于
?著作權(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)容

  • 為什么使用線程池 當(dāng)我們?cè)谑褂镁€程時(shí),如果每次需要一個(gè)線程時(shí)都去創(chuàng)建一個(gè)線程,這樣實(shí)現(xiàn)起來(lái)很簡(jiǎn)單,但是會(huì)有一個(gè)問(wèn)題...
    閩越布衣閱讀 4,424評(píng)論 10 45
  • 線程池算是Android開(kāi)發(fā)中非常常用的一個(gè)東西了,只要涉及到線程的地方,大多數(shù)情況下都會(huì)涉及到線程池。Andro...
    鄭在學(xué)_blog閱讀 724評(píng)論 0 1
  • Why does the sun go on shining 太陽(yáng)為何依然照耀 Why does the sea ...
    Helenkeller閱讀 383評(píng)論 0 1
  • ]耿睿婕職業(yè)生涯規(guī)劃系列原創(chuàng)分享之六十六: 周末再次到鄭州參加職業(yè)生涯規(guī)劃講師班的培訓(xùn),一天的思維訓(xùn)練課,一天的科...
    睿婕生涯心理咨詢工作室閱讀 357評(píng)論 0 0
  • 今天早上看了會(huì)兒?jiǎn)卧~,但是過(guò)了一小會(huì)兒室友就來(lái)了,感覺(jué)效果一般,明天換去個(gè)僻靜的地方看單詞,等快上課再去教室吧 今...
    老謀閱讀 158評(píng)論 1 0

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