Java並發包之ThreadPoolExecutor

來源:互聯網
上載者:User

標籤:lex   inter   illegal   handler   max   null   factory   構造   dex   

概述

  ThreadPoolExecutor作為java.util.concurrent包對外提供基礎實現,以內部線程池的形式對外提供管理工作執行,線程調度,線程池管理等等服務。

構造參數

  corePoolSize  核心線程數。maximumPoolSize 最大線程數。keepAliveTime最大存活時間。unit時間單位。workQueue阻塞任務隊列。threadFactory建立線程工廠。handler當提交任務數超過maxmumPoolSize+workQueue之和時,任務會交給RejectedExecutionHandler來處理。

  1.當線程池小於corePoolSize時,新提交任務將建立一個新線程執行任務,即使此時線程池中存在空閑線程。

  2.當線程池達到corePoolSize時,新提交任務將被放入workQueue中,等待線程池中任務調度執行

  3.當workQueue已滿,且maximumPoolSize>corePoolSize時,新提交任務會建立新線程執行任務

  4.當提交任務數超過maximumPoolSize時,新提交任務由RejectedExecutionHandler處理

  5.當線程池中超過corePoolSize線程,空閑時間達到keepAliveTime時,關閉空閑線程

  6.當設定allowCoreThreadTimeOut(true)時,線程池中corePoolSize線程空閑時間達到keepAliveTime也將關閉

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

 

Java並發包之ThreadPoolExecutor

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.