線程類的 activeCount() 方法用於返回當前線程所在線程組中的活動線程數。返回的值隻是一個估計值,因為在此方法遍曆內部數據結構時,線程數可能會動態變化。
用法
public static int activeCount()
返回
它返回當前線程的線程組中的活動線程數。
示例
public class JavaActiveCountExp extends Thread
{
JavaActiveCountExp(String threadname, ThreadGroup tg)
{
super(tg, threadname);
start();
}
public void run()
{
System.out.println("running thread name is:"+Thread.currentThread().getName());
}
public static void main(String arg[])
{
// creating the thread group
ThreadGroup g1 = new ThreadGroup("parent thread group");
// creating a thread
JavaActiveCountExp t1 = new JavaActiveCountExp("Thread-1", g1);
// creating another thread
JavaActiveCountExp t2 = new JavaActiveCountExp("Thread-2", g1);
// checking the number of active thread
System.out.println("number of active thread:"+ g1.activeCount());
}
}
輸出:
number of active thread:2 running thread name is:Thread-1 running thread name is:Thread-2
相關用法
- Java Thread toString()用法及代碼示例
- Java Thread interrupted()用法及代碼示例
- Java Thread setDefaultUncaughtExceptionHandler()用法及代碼示例
- Java Thread suspend()用法及代碼示例
- Java Thread destroy()用法及代碼示例
- Java Thread holdLock()用法及代碼示例
- Java Thread getContextClassLoader()用法及代碼示例
- Java Thread setContextClassLoader()用法及代碼示例
- Java Thread sleep()用法及代碼示例
- Java Thread getThreadGroup()用法及代碼示例
- Java Thread isInterrupted()用法及代碼示例
- Java Thread enumerate()用法及代碼示例
- Java Thread notify()用法及代碼示例
- Java Thread resume()用法及代碼示例
- Java Thread isDaemon()用法及代碼示例
- Java Thread setDaemon()用法及代碼示例
- Java Thread getId()用法及代碼示例
- Java Thread setName()用法及代碼示例
- Java Thread getDefaultUncaughtExceptionHandler()用法及代碼示例
- Java Thread getState()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Thread activeCount() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。