久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

Java多線程導(dǎo)致CPU占用100%解決及線程池正確關(guān)閉方式

瀏覽:3日期:2022-08-11 16:50:04
簡介

情景:1000萬表數(shù)據(jù)導(dǎo)入內(nèi)存數(shù)據(jù)庫,按分頁大小10000查詢,多線程,15條線程跑。使用了ExecutorService executor = Executors.newFixedThreadPool(15)本地跑了一段時(shí)間后,發(fā)現(xiàn)電腦CPU逐漸升高,最后CPU占用100%卡死,內(nèi)存使用也高達(dá)80%。

排查問題

Debug 發(fā)現(xiàn)雖然創(chuàng)建了定長15的線程池,但是因?yàn)閿?shù)據(jù)量大,在For中循環(huán)分頁查詢的List會(huì)持續(xù)加入LinkedBlockingQueue()隊(duì)列中每一個(gè)等待的任務(wù),又加載了1萬的數(shù)據(jù)。所以不管是線程數(shù)的CPU搶占,還是內(nèi)存的消耗都是極高。所以是不是能夠控制等待隊(duì)列LinkedBlockingQueue的上限就可以了。

Java多線程導(dǎo)致CPU占用100%解決及線程池正確關(guān)閉方式

解決辦法

使用AtomicLong 統(tǒng)計(jì)線程是否完成,再執(zhí)行executor.submit()提交新的任務(wù)導(dǎo)隊(duì)列中。偽代碼如下:

private AtomicLong threadNum = new AtomicLong(0);public void init() throws Exception {ExecutorService executor = Executors.newFixedThreadPool(15);Integer total = accountMapper.selectCount(new QueryWrapper<>());Integer pageSize = 10000; // 頁大小Integer pageCount = (total + pageSize -1) / pageSize; // 總頁數(shù)for (Integer start = 1; start <= pageCount; start++) {List<Account> list = accountMapper.selectPage(new Page<>(start, pageSize), query).getRecords();//等待線程任務(wù)完成,設(shè)置30,可令運(yùn)行線程數(shù)為15,等待隊(duì)列線程數(shù)為15while (threadNum.get() >= 30){Thread.sleep(5000);}//開啟1個(gè)線程+1threadNum.incrementAndGet();executor.submit(() -> {try {// 處理業(yè)務(wù)dealMessage(list);// 任務(wù)完成 -1threadNum.decrementAndGet();} catch (Exception e) {e.printStackTrace();}});}executor.shutdown();executor.awaitTermination(1, TimeUnit.DAYS);}

效果就是CPU保持在15~45%之間,內(nèi)存占用也只有45%。

目前只想到這樣的方式,控制等待隊(duì)列LinkedBlockingQueue的上限,還有更好的方式請告知,感謝!

2021-02-03-分割線最近又用到了多線程開發(fā),發(fā)現(xiàn)了還是有很多方式控制的。簡單的使用java的Semaphore令牌限流控制也能實(shí)現(xiàn)。

多線程: 線程池必須關(guān)閉,main主線程才能結(jié)束(接口才會(huì)返回)finally { executorService.shutdown(); } 主線程等待保證多線程所有子線程任務(wù)執(zhí)行完畢,再結(jié)束。 -> executorService.awaitTermination(1, TimeUnit.DAYS); semaphore 令牌限流控制fixedThread線程池,本例子就是最多同時(shí)擁有2個(gè)線程進(jìn)行工作 fixedThread.execute() fixedThread.submit() 的差別除了后者可以返回結(jié)果外,后者還會(huì)catch掉異常信息,無法拋到主線程中。

public static void main(String[] args) { final List<String> tableNames = new ArrayList<>(); tableNames.add('a'); tableNames.add('b'); tableNames.add('c'); tableNames.add('d'); tableNames.add('e'); tableNames.add('f'); final Semaphore semaphore = new Semaphore(2); final ExecutorService fixedThread = Executors.newCachedThreadPool(); for (final String tableName : tableNames) {//阻塞,獲取令牌try { semaphore.acquire();} catch (InterruptedException e) { e.printStackTrace();}//dofixedThread.execute(() -> { //can throw ex log final ExecutorService executorService = Executors.newCachedThreadPool(); try {executorService.submit(() -> { //can’t throw ex log //int i = 1/0; System.out.println('tableName2:' + tableName);});//int i = 1/0;System.out.println('tableName:' + tableName); } catch (Exception e) {e.printStackTrace(); } finally {executorService.shutdown();try { executorService.awaitTermination(1, TimeUnit.DAYS);} catch (InterruptedException e) { e.printStackTrace();}semaphore.release();System.out.println('semaphore.release'); }}); } // 記得關(guān)閉線程池 fixedThread.shutdown(); try {fixedThread.awaitTermination(1, TimeUnit.DAYS); } catch (InterruptedException e) {e.printStackTrace(); } System.out.println('主線程...');}

打印結(jié)果

tableName:btableName2:btableName:atableName2:asemaphore.releasesemaphore.releasetableName:dtableName2:dtableName:csemaphore.releasetableName:etableName2:csemaphore.releasetableName:ftableName2:esemaphore.releasetableName2:fsemaphore.release主線程...

到此這篇關(guān)于Java多線程導(dǎo)致CPU占用100%解決及線程池正確關(guān)閉方式的文章就介紹到這了,更多相關(guān)Java多線程CPU占用100%內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 精品国产一区二区三区av片 | 久久久久九九九九 | 国产亚洲综合精品 | 国产激情在线视频 | 日韩精品一区二区三区中文字幕 | 91视频18 | 国产特级毛片 | 男女视频免费在线观看 | 国产视频黄在线观看 | 国产综合一区二区 | 中文字幕视频在线 | 一道本一二三区 | 国产一区二区三区欧美 | 亚洲精品视频在线播放 | 99久久久国产精品美女 | 91免费在线视频 | 久久精品久久久久 | 国产欧美网址 | 国产在线观看一区二区三区 | 欧美亚洲成人一区 | 久久这里只有国产精品 | 日韩精品久久久久久 | 亚洲精品66| 国产精品高清在线 | 久久99国产精品久久99大师 | 日本久久艹| 99精品一区二区三区 | 精品少妇一区二区三区在线播放 | 日韩免费 | 国产天天操 | 天堂一区 | 久草 在线 | 国产精品永久久久久久久久久 | 久久99精品国产99久久6男男 | 午夜成人免费视频 | av在线一区二区三区 | 男女18免费网站视频 | 午夜激情免费在线观看 | 91久久精品一区二区二区 | 天天干,夜夜操 | 精品国产精品三级精品av网址 |