国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Java多線程導致CPU占用100%解決及線程池正確關閉方式

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

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

排查問題

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

Java多線程導致CPU占用100%解決及線程池正確關閉方式

解決辦法

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

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();//等待線程任務完成,設置30,可令運行線程數(shù)為15,等待隊列線程數(shù)為15while (threadNum.get() >= 30){Thread.sleep(5000);}//開啟1個線程+1threadNum.incrementAndGet();executor.submit(() -> {try {// 處理業(yè)務dealMessage(list);// 任務完成 -1threadNum.decrementAndGet();} catch (Exception e) {e.printStackTrace();}});}executor.shutdown();executor.awaitTermination(1, TimeUnit.DAYS);}

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

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

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

多線程: 線程池必須關閉,main主線程才能結(jié)束(接口才會返回)finally { executorService.shutdown(); } 主線程等待保證多線程所有子線程任務執(zhí)行完畢,再結(jié)束。 -> executorService.awaitTermination(1, TimeUnit.DAYS); semaphore 令牌限流控制fixedThread線程池,本例子就是最多同時擁有2個線程進行工作 fixedThread.execute() fixedThread.submit() 的差別除了后者可以返回結(jié)果外,后者還會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'); }}); } // 記得關閉線程池 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主線程...

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

標簽: Java
相關文章:
主站蜘蛛池模板: 黄色wwwxxx| 国产在线拍揄自揄视精品不卡 | 成人欧美网站免费 | 久草在线看 | 国产高中生粉嫩无套第一次 | 性欧美高清久久久久久久 | 久久国产99 | 国产毛片久久久久久国产毛片 | www.夜夜骑.com | 毛片在线视频观看 | 亚洲综合色在线观看 | 成人免费观看视频久爱网 | 国产亚洲欧美日韩在线观看不卡 | 国产精品国产三级国产专区5o | 最近中文在线中文 | 91精品国产免费久久久久久青草 | 国产在线精品一区免费香蕉 | 国产精品系列在线一区 | 久久久久久免费播放一级毛片 | 精品玖玖玖视频在线观看 | 国产精品久久久久久一区二区三区 | 亚洲第一区视频 | 免费在线一区二区三区 | 欧美大片一区二区三区 | 欧美做爰孕妇群 | 黄色理论视频 | 亚洲国产2017男人a天堂 | 国产一区二区精品久 | 三级黄色片网址 | 91久久精品国产一区二区 | 亚洲欧美精品国产一区色综合 | 在线观看国产精品日本不卡网 | 亚洲精品98久久久久久中文字幕 | 中文字幕国产亚洲 | 国产精品亚洲欧美日韩区 | 国产亚洲一区二区三区在线 | 青久草视频 | 美国三级网站 | 国产精品久久久久久免费 | 亚洲国产精品第一区二区三区 | 萌白酱粉嫩jk福利视频在线观看 |