更新時(shí)間:2020-07-17 16:12:41 來源:動(dòng)力節(jié)點(diǎn) 瀏覽3008次
線程是程序開發(fā)的基礎(chǔ),在平時(shí)的開發(fā)中也是必不可少的,特別是對(duì)多線程和線程池都會(huì)經(jīng)常運(yùn)用到。今天給大家介紹java中幾種常用的線程池,并了解每一種線程池的應(yīng)用場(chǎng)景。
線程使用的demo
public?static?void?cache()?{
????????ExecutorService?pool?=?Executors.newCachedThreadPool();
????????long?start?=?System.currentTimeMillis();
????????pool.execute(()?->?{
????????????int?sum?=?0;
????????????for?(int?i?=?0;?i?<?10;?i++)?{
????????????????sum?=?(int)?Math.sqrt(i?*?i?-?1?+?i);
????????????????System.out.println(sum);
????????????}
????????});
????????System.out.println("cache:?"?+?(System.currentTimeMillis()?-?start));
????}
newCachedThreadPool
重用之前的線程
適合執(zhí)行許多短期異步任務(wù)的程序。
調(diào)用execute()將重用以前構(gòu)造的線程
如果沒有可用的線程,則創(chuàng)建一個(gè)新線程并添加到池中
默認(rèn)為60s未使用就被終止和移除
長期閑置的池將會(huì)不消耗任何資源
源碼:
public?static?ExecutorService?newCachedThreadPool()?{
????????return?new?ThreadPoolExecutor(0,?Integer.MAX_VALUE,
??????????????????????????????????????60L,?TimeUnit.SECONDS,
??????????????????????????????????????new?SynchronousQueue());
????}
通過源碼可以看出底層調(diào)用的是ThreadPoolExecutor方法,傳入一個(gè)同步的阻塞隊(duì)列實(shí)現(xiàn)緩存。
下面說一下ThreadPoolExecutor
public?ThreadPoolExecutor(int?corePoolSize,
??????????????????????????????int?maximumPoolSize,
??????????????????????????????long?keepAliveTime,
??????????????????????????????TimeUnit?unit,
??????????????????????????????BlockingQueue?workQueue)?{
????????this(corePoolSize,?maximumPoolSize,?keepAliveTime,?unit,?workQueue,
?????????????Executors.defaultThreadFactory(),?defaultHandler);
????}
通過源碼可以看出,我們可以傳入線程池的核心線程數(shù)(最小線程數(shù)),最大線程數(shù)量,保持時(shí)間,時(shí)間單位,阻塞隊(duì)列這些參數(shù),最大線程數(shù)設(shè)置為jvm可用的cpu數(shù)量為最佳實(shí)踐
newWorkStealingPool
獲取當(dāng)前可用的線程數(shù)量進(jìn)行創(chuàng)建作為并行級(jí)別
使用ForkJoinPool
源碼:
public?static?ExecutorService?newWorkStealingPool()?{
????????return?new?ForkJoinPool
????????????(Runtime.getRuntime().availableProcessors(),
?????????????ForkJoinPool.defaultForkJoinWorkerThreadFactory,
?????????????null,?true);
????}
通過源碼可以看出底層調(diào)用的是ForkJoinPool線程池
下面說一下ForkJoinPool
public?ForkJoinPool(int?parallelism,
????????????????????????ForkJoinWorkerThreadFactory?factory,
????????????????????????UncaughtExceptionHandler?handler,
????????????????????????boolean?asyncMode)?{
????????this(checkParallelism(parallelism),
?????????????checkFactory(factory),
?????????????handler,
?????????????asyncMode???FIFO_QUEUE?:?LIFO_QUEUE,
?????????????"ForkJoinPool-"?+?nextPoolId()?+?"-worker-");
????????checkPermission();
????}
使用一個(gè)無限隊(duì)列來保存需要執(zhí)行的任務(wù),可以傳入線程的數(shù)量,不傳入,則默認(rèn)使用當(dāng)前計(jì)算機(jī)中可用的cpu數(shù)量,使用分治法來解決問題,使用fork()和join()來進(jìn)行調(diào)用。
以上就是動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)的小編針對(duì)“javase基礎(chǔ)入門教程之線程池的幾種應(yīng)用場(chǎng)景”的內(nèi)容進(jìn)行的回答,希望對(duì)大家有所幫助,如有疑問,請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)