Android中的線程池

線程池有以下的優(yōu)點:

  • 重用線程池中的線程,避免因為線程的創(chuàng)建和銷毀所帶來的性能上的開銷
  • 能有效控制線程池的最大并發(fā)數(shù),避免大量的線程之間因為互相搶占系統(tǒng)資源而導(dǎo)致阻塞現(xiàn)象
  • 能夠?qū)€程池就行簡單的管理,并提供定時執(zhí)行以及指定間隔循環(huán)
線程池介紹

ThreadPoolExecutor是線程池的真正實現(xiàn)

  /**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters and default rejected execution handler.
     *
     * @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
     * @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} is null
     */
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             threadFactory, defaultHandler);
    }

ThreadPoolExecutor執(zhí)行任務(wù)是遵循的規(guī)則


image.png

參數(shù)設(shè)置

private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
    private static final int KEEP_ALIVE = 1;

    private static final ThreadFactory sThreadFactory = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        public Thread newThread(Runnable r) {
            return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
        }
    };

    private static final BlockingQueue<Runnable> sPoolWorkQueue =
            new LinkedBlockingQueue<Runnable>(128);

    /**
     * An {@link Executor} that can be used to execute tasks in parallel.
     */
    public static final Executor THREAD_POOL_EXECUTOR
            = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
                    TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);

線程池的分類

·FixedThreadPool:
數(shù)量固定的核心線程池,沒有超時機制。任務(wù)隊列大小沒有限制。
·CachedThreadPool:
線程數(shù)量不定,只有非核心賢臣,最大線程數(shù)位Integer.MAX_VALUE。它比較適合執(zhí)行大量的耗時較少的任務(wù)
·ScheduledThreadPool:
核心線程數(shù)量固定,非核心線程數(shù)量沒有限制。適合執(zhí)行定時任務(wù)和具有固定周期的重復(fù)任務(wù)。
·SingleThreadExecutor:
只有一個核心線程

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

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

  • 線程池的優(yōu)點: 重用線程池中的線程,避免因為線程的創(chuàng)建和銷毀帶來的性能消耗 能有效的控制線程的最大并發(fā)數(shù),避免大量...
    乆丩乣閱讀 5,493評論 5 30
  • 很久沒有更新了,首先跟各位說聲對不起。近段時間陷進了失眠的深淵難以自拔,工作、生活一團糟,每天都是滿滿的負(fù)能量,學(xué)...
    山野紙鶴閱讀 1,493評論 0 9
  • 線程池的好處 (1)重用線程池中的線程,避免因為線程的創(chuàng)建和銷毀所帶來的性能的開銷。(2)能有效控制線程池的最大并...
    藍(lán)楓zeke閱讀 389評論 0 2
  • Android 中的四種線程池 在開發(fā)中使用線程池的優(yōu)點 重用線程池中的線程,避免因為線程的創(chuàng)建和銷毀帶來的性能開...
    任教主來也閱讀 253評論 0 0
  • 猝不及防,史上首次一條政治新聞引發(fā)起姐妹界的瘋轉(zhuǎn),標(biāo)題是:“殺害金正男特工身份曝光,疑似1988年中年女子”。 原...
    魏費斯閱讀 1,547評論 15 4

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