JAVA 創(chuàng)建線程池的注意事項(xiàng)
1、創(chuàng)建線程或線程池時(shí)請(qǐng)指定有意義的線程名稱,方便出錯(cuò)時(shí)回溯。創(chuàng)建線程池的時(shí)候請(qǐng)使用帶ThreadFactory的構(gòu)造函數(shù),并且提供自定義ThreadFactory實(shí)現(xiàn)或者使用第三方實(shí)現(xiàn)。
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat('demo-pool-%d').build();ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());singleThreadPool.execute(()-> System.out.println(Thread.currentThread().getName()));singleThreadPool.shutdown();public class TimerTaskThread extends Thread {public TimerTaskThread(){super.setName('TimerTaskThread'); …}
2、線程池不允許使用Executors去創(chuàng)建,而是通過(guò)ThreadPoolExecutor的方式,這樣的處理方式讓寫的同學(xué)更加明確線程池的運(yùn)行規(guī)則,規(guī)避資源耗盡的風(fēng)險(xiǎn)。
說(shuō)明:Executors返回的線程池對(duì)象的弊端如下:
1)FixedThreadPool和SingleThreadPool: 允許的請(qǐng)求隊(duì)列長(zhǎng)度為Integer.MAX_VALUE,可能會(huì)堆積大量的請(qǐng)求,從而導(dǎo)致OOM。
2)CachedThreadPool: 允許的創(chuàng)建線程數(shù)量為Integer.MAX_VALUE,可能會(huì)創(chuàng)建大量的線程,從而導(dǎo)致OOM。
Positive example 1:
//org.apache.commons.lang3.concurrent.BasicThreadFactoryScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,new BasicThreadFactory.Builder().namingPattern('example-schedule-pool-%d').daemon(true).build());
Positive example 2:
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat('demo-pool-%d').build();//Common Thread PoolExecutorService pool = new ThreadPoolExecutor(5, 200,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());pool.execute(()-> System.out.println(Thread.currentThread().getName()));pool.shutdown();//gracefully shutdown
Positive example 3:
<bean id='userThreadPool'class='org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor'><property name='corePoolSize' value='10' /><property name='maxPoolSize' value='100' /><property name='queueCapacity' value='2000' /><property name='threadFactory' value= threadFactory /><property name='rejectedExecutionHandler'><ref local='rejectedExecutionHandler' /></property></bean>//in codeuserThreadPool.execute(thread);
3、線程資源必須通過(guò)線程池提供,不允許在應(yīng)用中自行顯式創(chuàng)建線程。
說(shuō)明:
使用線程池的好處是減少在創(chuàng)建和銷毀線程上所花的時(shí)間以及系統(tǒng)資源的開(kāi)銷,解決資源不足的問(wèn)題。
如果不使用線程池,有可能造成系統(tǒng)創(chuàng)建大量同類線程而導(dǎo)致消耗完內(nèi)存或者“過(guò)度切換”的問(wèn)題。
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat('demo-pool-%d').build();ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());singleThreadPool.execute(()-> System.out.println(Thread.currentThread().getName()));singleThreadPool.shutdown();
以上就是JAVA 創(chuàng)建線程池的注意事項(xiàng)的詳細(xì)內(nèi)容,更多關(guān)于JAVA 創(chuàng)建線程池注意事項(xiàng)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python 合并拼接字符串的方法2. python使用jenkins發(fā)送企業(yè)微信通知的實(shí)現(xiàn)3. Python3 json模塊之編碼解碼方法講解4. 通過(guò)實(shí)例解析Python文件操作實(shí)現(xiàn)步驟5. ASP基礎(chǔ)知識(shí)VBScript基本元素講解6. Python 制作查詢商品歷史價(jià)格的小工具7. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條8. PHP使用Swagger生成好看的API文檔9. Python 利用Entrez庫(kù)篩選下載PubMed文獻(xiàn)摘要的示例10. Python 如何調(diào)試程序崩潰錯(cuò)誤

網(wǎng)公網(wǎng)安備