從本課開始學(xué)習(xí)并發(fā)編程的內(nèi)容。主要介紹并發(fā)編程的基礎(chǔ)知識(shí)、鎖、內(nèi)存模型、線程池、各種并發(fā)容器的使用。
線程池
Executors
Executor
ExecutorService
ThreadPoolExecutor
有以下幾個(gè)原因:
根本上說(shuō),我們使用線程池主要就是為了減少創(chuàng)建和銷毀線程的次數(shù),每個(gè)線程 都可以重復(fù)利用,可以執(zhí)行多個(gè)任務(wù),從而節(jié)省內(nèi)存(線程開的越多,消耗的內(nèi)存越大),提高了資源利用率。
ThreadPoolExecutor
代碼起始是Executor
,這是一個(gè)接口。
抽象類AbstractExecutorService
實(shí)現(xiàn)了接口ExecutorService
,ExecutorService
又繼承了Executor
,AbstractExecutorService
的默認(rèn)實(shí)現(xiàn)類是ThreadPoolExecutor
,這個(gè)類就是線程池。
ThreadPoolExecutor
有4個(gè)構(gòu)造函數(shù):
public ThreadPoolExecutor(int corePoolSize,
int maximunPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Running> workQueue);
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Running> workQueue,
ThreadFactory factory);
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Running> workQueue,
RejectedExecutionHandler handler);
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Running> workQueue,
ThreadFactory factory,
RejectedExecutionHandler handler);
corePoolSize
:線程池中核心線程的大數(shù)量
核心線程,線程池創(chuàng)建后,如果當(dāng)前線程總數(shù)小于corePoolSize,則新建的就是核心線程,如果超過(guò)corePoolSize,則新建的就是非核心線程。
核心線程默認(rèn)情況下會(huì)一直存活在線程池中,即使這個(gè)核心線程處于閑置狀態(tài)。
如果指定ThreadPoolExecutor的allowCoreThreadTimeOut
為true,那么核心線程如果處于閑置狀態(tài)的話,超過(guò)一定時(shí)間(keepAliveTime)也會(huì)被銷毀。
maximumPoolSize
: 線程池中線程總數(shù)的大值
線程總數(shù) = 核心線程數(shù) + 非核心線程數(shù)
keepAliveTime
: 線程池中非核心線程閑置超時(shí)時(shí)長(zhǎng)
默認(rèn)情況下,一個(gè)非核心線程,如果閑置時(shí)長(zhǎng)超過(guò)該參數(shù)設(shè)置,就會(huì)被銷毀。如果設(shè)置參數(shù)allowCoreThreadTimeOut為true,則超時(shí)時(shí)長(zhǎng)也會(huì)作用于核心線程。
TimeUnit unit
: 時(shí)長(zhǎng)單位
枚舉值,MILLSECONDS
:毫秒;SECONDS
:秒;MINUTS
:分鐘;HOURS
:小時(shí);DAYS
:天
workQueue: 阻塞隊(duì)列
線程池中的任務(wù)隊(duì)列,維護(hù)著等待執(zhí)行的Runnable對(duì)象。當(dāng)所有的核心線程都在干活時(shí),新添加的任務(wù)會(huì)被添加到這個(gè)隊(duì)列中等待處理,如果這個(gè)隊(duì)列滿了,則會(huì)新建非核心線程來(lái)執(zhí)行任務(wù)。
public interface BlockingQueue<E> extends Queue<E> {
// 將指定元素添加到隊(duì)列,成功返回true,否則拋出異常;如果是給限定了長(zhǎng)度的隊(duì)列中添加元素,推薦offer
boolean add(E e);
// 將指定的元素添加的隊(duì)列,如果成功則返回true,否則返回false;元素不能為空,否則拋出NPE
boolean offer(E e);
// 將元素添加到隊(duì)列,如果隊(duì)列沒(méi)有多余空間,方法會(huì)一直阻塞,直到隊(duì)列有多余空間
boolean put(E e) throws InterruptedException;
// 將元素在指定的時(shí)間內(nèi)添加到隊(duì)列中,成功返回true,否則返回false
boolean off(E e, long timeout, TimeUnit unit) throws InterruptedException;
// 從隊(duì)列中獲取值,如果隊(duì)列沒(méi)有元素,方法會(huì)一直阻塞,直到隊(duì)列有值
E take() throws InterruptedException;
// 在給定的時(shí)間內(nèi)獲取隊(duì)列中的值,沒(méi)有獲取到會(huì)拋出異常
E poll(long timeout, TimeUnit unit) throws InterruptedException;
// 獲取隊(duì)列的剩余空間
int remainingCapacity();
// 移除指定的值
boolean remove(Object o);
// 判斷隊(duì)列是否包含值
boolean contains(Object o);
// 將隊(duì)列中的所有元素都移除,并設(shè)置到指定集合中
int drainTo(Collection<? extends E> c);
}
一般來(lái)說(shuō),workQueue有以下四種隊(duì)列類型:
SynchronousQueue
:同步隊(duì)列,這種隊(duì)列在接收到任務(wù)時(shí),會(huì)直接提交給線程處理,而不會(huì)保留它。假如所有線程都在忙碌,則會(huì)新建線程來(lái)處理這個(gè)任務(wù)。所以為了防止出現(xiàn)線程數(shù)達(dá)到maximumPoolSize而不能新建線程的錯(cuò)誤,當(dāng)使用這種隊(duì)列時(shí),需要把maximumPoolSize指定為Integer.MAX_VALUE。
LinkedBlockingQueue
:×××鏈表阻塞隊(duì)列,這種隊(duì)列接收到任務(wù)時(shí),如果當(dāng)前線程數(shù)少于核心線程數(shù),則會(huì)新建核心線程來(lái)處理任務(wù);如果當(dāng)前線程數(shù)達(dá)到核心線程數(shù),則會(huì)保持到該隊(duì)列中;由于隊(duì)列×××,即所有超過(guò)核心線程數(shù)的任務(wù)都會(huì)存入隊(duì)列,所以會(huì)導(dǎo)致maximumPoolSize失效。
ArrayBlockingQueue
:有界數(shù)組阻塞隊(duì)列,接收到任務(wù)時(shí),如果當(dāng)前線程數(shù)少于核心線程數(shù),則會(huì)新建核心線程來(lái)處理任務(wù);如果當(dāng)前線程數(shù)達(dá)到核心線程數(shù),則會(huì)保持到該隊(duì)列中;如果隊(duì)列已滿,則新建線程來(lái)執(zhí)行任務(wù),如果隊(duì)列已滿,而且線程數(shù)已達(dá)到maximumPoolSize指定的數(shù)量,則會(huì)報(bào)錯(cuò)。
DelayQueue
:延遲隊(duì)列,隊(duì)列元素必須實(shí)現(xiàn)Delayed
接口,接收到任務(wù)時(shí),先入隊(duì)列,只有達(dá)到了指定時(shí)間,才會(huì)執(zhí)行任務(wù)。
ThreadFactory factory
: 創(chuàng)建線程的方式
這是一個(gè)接口,通過(guò)調(diào)用它的方法:Thread newThread(Runnable r)
來(lái)創(chuàng)建線程
RejectedExecutionHandler handler
: 線程池?zé)o法創(chuàng)建線程時(shí),如何拋出異常
一般是當(dāng)線程池中的線程數(shù)量已經(jīng)達(dá)到大值,或者線程池已經(jīng)關(guān)閉時(shí),會(huì)拋出一個(gè)RejectedExecutionException
既然線程池新添加了任務(wù),那么線程池是如何處理這些批量任務(wù)?
1newFixedThreadPool
:定長(zhǎng)的線程池,可控制線程的大并發(fā)數(shù),超出的任務(wù)會(huì)在隊(duì)列中等待。
public class Executors {
public static ExecutorService newFixedThreadPool(int nThread) {
return new ThreadPoolExecutor(nThread, nThread, 0L, TimeUnit.MILLSECONDS, new LinkedBlockingQueue<Runnable>());
}
}
2newCachedThreadPool
:可緩存的線程池,如果線程池長(zhǎng)度超過(guò)需要,可以靈活回收空閑線程,如無(wú)可回收線程,則會(huì)新建線程來(lái)處理任務(wù)。
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
}
3newScheduledThreadPool
:定長(zhǎng)任務(wù)線程池,支持定時(shí)和周期性任務(wù)執(zhí)行。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
4newSingleThreadExecutor
:一個(gè) 單線程的線程池,只有一個(gè)線程來(lái)執(zhí)行任務(wù)。
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService(
new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLSECONDS, new LinkedBlockingQueue<Runnable>());
);
}
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。
分享名稱:掌握系列之并發(fā)編程-9.線程池-創(chuàng)新互聯(lián)
文章分享:http://chinadenli.net/article26/doeojg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站排名、網(wǎng)站設(shè)計(jì)公司、微信小程序、外貿(mào)建站、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
移動(dòng)網(wǎng)站建設(shè)知識(shí)