新聞中心
在實際開發(fā)過程中,在線程池使用過程中可能會遇到各方面的故障,如線程池阻塞,無法提交新任務等。
成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務,包含不限于成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、新城網(wǎng)絡推廣、成都微信小程序、新城網(wǎng)絡營銷、新城企業(yè)策劃、新城品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)公司為所有大學生創(chuàng)業(yè)者提供新城建站搭建服務,24小時服務熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
如果你想監(jiān)控某一個線程池的執(zhí)行狀態(tài),線程池執(zhí)行類 ThreadPoolExecutor 也給出了相關(guān)的 API, 能實時獲取線程池的當前活動線程數(shù)、正在排隊中的線程數(shù)、已經(jīng)執(zhí)行完成的線程數(shù)、總線程數(shù)等。
總線程數(shù) = 排隊線程數(shù) + 活動線程數(shù) + 執(zhí)行完成的線程數(shù)。
線程池使用示例:
private static ExecutorService es = new ThreadPoolExecutor(50, 100, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(100000)); public static void main(String[] args) throws Exception { for (int i = 0; i < 100000; i++) { es.execute(() -> { System.out.print(1); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }); } ThreadPoolExecutor tpe = ((ThreadPoolExecutor) es); while (true) { System.out.println(); int queueSize = tpe.getQueue().size(); System.out.println("當前排隊線程數(shù):" + queueSize); int activeCount = tpe.getActiveCount(); System.out.println("當前活動線程數(shù):" + activeCount); long completedTaskCount = tpe.getCompletedTaskCount(); System.out.println("執(zhí)行完成線程數(shù):" + completedTaskCount); long taskCount = tpe.getTaskCount(); System.out.println("總線程數(shù):" + taskCount); Thread.sleep(3000); } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
新聞名稱:Java線程池運行狀態(tài)監(jiān)控實現(xiàn)解析
標題網(wǎng)址:http://fisionsoft.com.cn/article/jcgdhi.html