線程池📣 📣 📣 📢📢📢
成都創(chuàng)新互聯(lián)公司于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元青海做網(wǎng)站,已為上家服務(wù),為青海各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108
????你好啊!小伙伴,我是小冷。是一個興趣驅(qū)動自學(xué)練習(xí)兩年半的的Java工程師。
📒 一位十分喜歡將知識分享出來的Java博主??????,擅長使用Java技術(shù)開發(fā)web項(xiàng)目和工具
📒 文章內(nèi)容豐富:覆蓋大部分java必學(xué)技術(shù)棧,前端,計(jì)算機(jī)基礎(chǔ),容器等方面的文章
📒 如果你也對Java感興趣,關(guān)注小冷吧,一起探索Java技術(shù)的生態(tài)與進(jìn)步,一起討論Java技術(shù)的使用與學(xué)習(xí)
??高質(zhì)量技術(shù)專欄專欄鏈接: 微服務(wù),數(shù)據(jù)結(jié)構(gòu),netty,單點(diǎn)登錄,SSM ,SpringCloudAlibaba等
😝公眾號😝 : 想全棧的小冷,分享一些技術(shù)上的文章,以及解決問題的經(jīng)驗(yàn)
?當(dāng)前專欄:JUC系列
池化技術(shù)、
程序的運(yùn)行 本質(zhì): 占用系統(tǒng)的資源 ! 優(yōu)化資源的使用 =>池化技術(shù)
線程池,連接池,內(nèi)存吃,對象池, 頻繁的創(chuàng)建銷毀 十分的浪費(fèi)資源
線程池的好處:
線程的復(fù)用 可以控制大并發(fā)數(shù)量,管理線程
三大方法下圖來自 阿里巴巴開發(fā)規(guī)約手冊
代碼實(shí)例
public class poolDemo {public static void main(String[] args) {//單個線程
ExecutorService Threadpool = Executors.newSingleThreadExecutor();
// 創(chuàng)建一個固定的線程池大小
//ExecutorService Threadpool = Executors.newFixedThreadPool(5);
//可以伸縮的 遇強(qiáng)則強(qiáng)
// ExecutorService Threadpool = Executors.newCachedThreadPool();
try {for (int i = 0; i< 10; i++) {Threadpool.execute(() ->{System.out.println(Thread.currentThread().getName() + "=>ok");
});
}
} catch (Exception e) {e.printStackTrace();
} finally {//線程池使用完畢 一定要關(guān)閉
Threadpool.shutdown();
}
}
}
三大方法的創(chuàng)建代碼
public static ExecutorService newSingleThreadExecutor() {return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue()));
}
public static ExecutorService newFixedThreadPool(int nThreads) {return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue());
}
public static ExecutorService newCachedThreadPool() {return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue());
}
七大參數(shù)七大參數(shù)
public ThreadPoolExecutor(int corePoolSize, //核心線程池大小
int maximumPoolSize,// 大核心線程數(shù)大小
long keepAliveTime,// 超時了沒有人調(diào)用就會釋放
TimeUnit unit,// 超時單位
BlockingQueueworkQueue,// 阻塞隊(duì)列
ThreadFactory threadFactory, // 線程工廠,創(chuàng)建線程的
RejectedExecutionHandler handler// 拒絕策略
)
這七個參數(shù)分別有什么作用呢,思路圖
手動創(chuàng)建線程池,不用封裝好的方法,使用原生的線程池方法
ThreadPoolExecutor threadpool = new ThreadPoolExecutor(
2,
5,
3,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(3),
Executors.defaultThreadFactory(),
// 這個時候 舉例子,銀行的人滿了 這種方式就是 不處理 拋出異常
new ThreadPoolExecutor.AbortPolicy()
);
線程池四種拒絕策略new ThreadPoolExecutor.AbortPolicy() 銀行的人滿了 這種方式就是 不處理 拋出異常
執(zhí)行效果
new ThreadPoolExecutor.CallerRunsPolicy() 銀行人滿了,哪里來的去哪里,回到調(diào)用線程輸出,不會異常
執(zhí)行結(jié)果
new ThreadPoolExecutor.DiscardPolicy() 隊(duì)列滿了 就拋出全部任務(wù),
執(zhí)行結(jié)果
new ThreadPoolExecutor.DiscardOldestPolicy() 嘗試和最早的線程競爭 查看是否有位置,沒有就拋出任務(wù)
執(zhí)行結(jié)果
代碼實(shí)例
public class poolDemo {public static void main(String[] args) {ThreadPoolExecutor threadpool = new ThreadPoolExecutor(
2,
5,
3,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.DiscardOldestPolicy()
);
//單個線程
//ExecutorService Threadpool = Executors.newSingleThreadExecutor();
// 創(chuàng)建一個固定的線程池大小
//ExecutorService Threadpool = Executors.newFixedThreadPool(5);
//可以伸縮的 遇強(qiáng)則強(qiáng)
// ExecutorService Threadpool = Executors.newCachedThreadPool();
try {//大承載如何計(jì)算 : 阻塞隊(duì)列+max數(shù)量
//超過的話就會 爆出異常 :RejectedExecutionException
for (int i = 1; i<= 15; i++) {threadpool.execute(() ->{System.out.println(Thread.currentThread().getName() + "=>ok");
});
}
} catch (Exception e) {e.printStackTrace();
} finally {//線程池使用完畢 一定要關(guān)閉
threadpool.shutdown();
}
}
}
大負(fù)載CPU密集型 有幾個核心就定義幾個,可以保證效率最高
Runtime.getRuntime().availableProcessors() //獲取cpu 核心數(shù)
IO 密集型 判斷程序中 十分消耗IO資源的線程,如: 程序 有 15個大型任務(wù),io 十分占中資源,那么設(shè)定的比任務(wù)數(shù)量大 就可以保證一定性能、
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
文章標(biāo)題:JUC系列(六)線程池-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://bm7419.com/article46/cdejhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站維護(hù)、網(wǎng)站導(dǎo)航、做網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容