public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory)
corePoolSize
核心線程數(shù)目,默認(rèn),核心線程會(huì)一直存活哪审,除非ThreadPoolExecutor的allowCoreThreadTimeOut屬性為true够掠。
maximumPoolSize
線程池能容納的最大線程數(shù)量
keepAliveTime
非核心線程的閑置時(shí)長(zhǎng),當(dāng)allowCoreThreadTimeOut屬性為true,對(duì)核心線程也有效。
unit
指定keepAliveTime的時(shí)間單位,枚舉耸峭,毫秒TimeUnit.MILLISECONDS,秒TimeUnit.Second,分TimeUnit.MINUTES。
workQueue
任務(wù)隊(duì)列淋纲,通過(guò)execute提交的Runnable儲(chǔ)存在這
threadFactory
為線程池提供創(chuàng)建線程的功能劳闹。
FixedThreadPool
ExecutorService pool= Executors.newFixedThreadPool();
線程數(shù)目固定,只有核心線程數(shù)(不會(huì)被回收)洽瞬,當(dāng)所以線程處于活動(dòng)時(shí)本涕,新任務(wù)會(huì)處于等待狀態(tài)。能快速反應(yīng)外界請(qǐng)求
CachedThreadPool
ExecutorService pool= Executors.newCachedThreadPool();
只有非核心線程數(shù)目伙窃,超時(shí)60s偏友,線程數(shù)目不固定,適合執(zhí)行大量消耗時(shí)間少的任務(wù)对供。
ScheduledThreadPool
1.ExecutorService pool= Executors.newScheduledThreadPool(3);
2.ScheduledExecutorService pool=Executor.newScheduledThreadPool(3);
//延遲2秒后執(zhí)行command
pool.schedule(command,2000,TimeUnit.MILLISECONDS);
//延遲1秒后每2秒執(zhí)行一次command
pool.scheduleAtFixedRate(command,1000,2000,TimeUnit.MILLISECONDS);
核心線程數(shù)目固定位他。非核心線程數(shù)目不定,閑置立即被回收产场,適合執(zhí)行定時(shí)或者固定周期任務(wù)鹅髓。
ExecutorService
ExecutorService pool= Executors.newSingleThreadExecutor();
只有一個(gè)核心線程,所有任務(wù)排隊(duì)執(zhí)行京景。