Java實(shí)現(xiàn)監(jiān)控多個(gè)線程狀態(tài)的簡(jiǎn)單實(shí)例
實(shí)例如下:
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
* 測(cè)試監(jiān)控類
*
* @author
*
*/
public class WatchThread {
/**
* 測(cè)試函數(shù)
*
* @throws InterruptedException
*/
public void testThread() throws InterruptedException {
int threadNum = 10;
// 初始化countDown
CountDownLatch threadSignal = new CountDownLatch(threadNum);
// 創(chuàng)建固定長(zhǎng)度的線程池
Executor executor = Executors.newFixedThreadPool(threadNum);
for (int i = 0; i < threadNum; i++) { // 開threadNum個(gè)線程
Runnable task = new TestThread(threadSignal);
// 執(zhí)行
executor.execute(task);
}
threadSignal.await(); // 等待所有子線程執(zhí)行完
// do work
System.out.println(Thread.currentThread().getName() + "+++++++結(jié)束.");
}
/**
* 測(cè)試函數(shù)
*/
public static void main(String[] args) throws InterruptedException {
WatchThread test = new WatchThread();
test.testThread();
}
/**
*
* @author jill
*
*/
private class TestThread implements Runnable {
private CountDownLatch threadsSignal;
public TestThread(CountDownLatch threadsSignal) {
this.threadsSignal = threadsSignal;
}
public void run() {
System.out.println(Thread.currentThread().getName() + "開始...");
// do shomething
System.out.println("開始了線程::::" + threadsSignal.getCount());
// 線程結(jié)束時(shí)計(jì)數(shù)器減1
threadsSignal.countDown(); //這句代碼 建議放在 finally里執(zhí)行
System.out.println(Thread.currentThread().getName() + "結(jié)束. 還有"
+ threadsSignal.getCount() + " 個(gè)線程");
}
}
}
以上這篇Java實(shí)現(xiàn)監(jiān)控多個(gè)線程狀態(tài)的簡(jiǎn)單實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis入門指南之實(shí)現(xiàn)對(duì)數(shù)據(jù)庫增刪改查
數(shù)據(jù)持久層主要負(fù)責(zé)數(shù)據(jù)的增、刪、改、查等功能,MyBatis 則是一款優(yōu)秀的持久層框架,下面這篇文章主要給大家介紹了關(guān)于Mybatis入門指南之實(shí)現(xiàn)對(duì)數(shù)據(jù)庫增刪改查的相關(guān)資料,需要的朋友可以參考下2022-10-10
IDEA最新激活碼2021(IDEA2020.3.2最新永久激活方法)
這篇文章主要介紹了IDEA最新激活碼2021(IDEA2020.3.2最新永久激活方法),本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Springboot+Poi導(dǎo)入Excel表格實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Springboot+Poi導(dǎo)入Excel表格實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
java中線程的sleep()方法和yield()方法的區(qū)別
本文主要介紹了java中線程的sleep()方法和yield()方法的區(qū)別,Thread類的sleep()方法使線程休眠指定時(shí)間,不釋放鎖,而yield()提示調(diào)度器當(dāng)前線程愿意讓出CPU資源,不保證立即切換線程,感興趣的可以了解一下2024-10-10
Java8中的LocalDateTime和Date一些時(shí)間操作方法
這篇文章主要介紹了Java8中的LocalDateTime和Date一些時(shí)間操作方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Spring負(fù)載均衡LoadBalancer使用詳解
這篇文章主要介紹了Spring負(fù)載均衡LoadBalancer使用詳解,Spring Cloud LoadBalancer是Spring Cloud官方自己提供的客戶端負(fù)載均衡器, 用來替代Ribbon,Spring官方提供了兩種客戶端都可以使用loadbalancer,需要的朋友可以參考下2023-11-11
IDEA調(diào)試功能使用總結(jié)(step?over/step?into/force?step?into/step?o
本文主要介紹了IDEA調(diào)試功能使用總結(jié)(step?over/step?into/force?step?into/step?out),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
SpringBoot實(shí)現(xiàn)郵件發(fā)送功能的姿勢(shì)分享
我們?cè)谌粘i_發(fā)中,經(jīng)常會(huì)碰到email郵件發(fā)送的場(chǎng)景,如發(fā)送驗(yàn)證碼,向客戶發(fā)送郵件等等,這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)郵件發(fā)送的相關(guān)資料,需要的朋友可以參考下2021-08-08
Java生成日期時(shí)間存入Mysql數(shù)據(jù)庫的實(shí)現(xiàn)方法
本文主要介紹了Java生成日期時(shí)間存入Mysql數(shù)據(jù)庫的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

