Executor框架?
靈活強大的異步框架,支持多種不同類型的任務執(zhí)行策略,將任務提交過程與執(zhí)行過程分離開來,解耦開發(fā)
Executor 接口 提供了提交任務的方法executor ?
ExecutorService 子接口 提供了生命周期管理方法,submit(Runnable),shutdown() ,shutdownNow()
?AbstractExecutorService ?ThreadPoolExecutor ?線程池利用工廠類創(chuàng)建各種線程池
ScheduledThreadPoolExecutor 可定時調(diào)度任務的線程池
ThreadPoolExecutor 主要參數(shù)
corePoolSize ?核心線程數(shù)
maximumPoolSize 最大線程數(shù)
long keepAliveTime ?非核心空閑線程存活時間
BlockingQueue<Runnable> ? workQueue ?保存任務的阻塞隊列
handle ? 拒絕策略
(1)ThreadPoolExecutor
newFixedThreadPool(n,n,0L,new linkedBlockingQueue<Runnable>);可重用的固定線程數(shù)線程池
newSingleThreadPoolExecutor(1,1,0L,linkedBlockingQueue<Runnable>);單線程的線程池適用于保證順序的執(zhí)行各個任務
newCachedThreadPool(0,Integer.Max_value,60L,synchronousQueue<Runnable>);
大小無界的線程池,適用于執(zhí)行很多短期異步任務的小程序
(2)ScheduledThreadPoolExecutor
newSingleThreadPoolExecutor(1,1,0L,DelayedWorkQueue);可調(diào)度單線程線程池
newScheduledThreadPoolExecutor();可調(diào)度的多線程池
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。