ThreadGroup 類的 activeCount() 方法用於返回當前線程的線程組中的活動線程數。返回值隻是一個估計值,因為在此方法遍曆內部數據結構時,線程數可能會動態變化。
用法
public static int activeCount()
返回
它返回當前線程的線程組中的活動線程數。
示例
class NewThread extends Thread
{
NewThread(String threadname, ThreadGroup tg)
{
super(tg, threadname);
}
public void run()
{
for (int i = 0; i < 10; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
System.out.println(Thread.currentThread().getName() + " interrupted");
}
}
System.out.println(Thread.currentThread().getName() + " completed executing");
}
}
public class ThreadGroupActiveCountExp
{
public static void main(String arg[])
{
// creating the thread group
ThreadGroup tg1 = new ThreadGroup("parent thread group");
// creating the threads
NewThread t1 = new NewThread("Thread-1", tg1);
NewThread t2 = new NewThread("Thread-2", tg1);
NewThread t3 = new NewThread("Thread-3", tg1);
// this will call the run() method
t1.start();
t2.start();
t3.start();
// checking the number of active thread
System.out.println("Number of active thread:"+ tg1.activeCount());
}
}
輸出:
Number of active thread:3 Thread-1 completed executing Thread-2 completed executing Thread-3 completed executing
相關用法
- Java ThreadGroup activeCount()用法及代碼示例
- Java ThreadGroup activeGroupCount()用法及代碼示例
- Java ThreadGroup enumerate()用法及代碼示例
- Java ThreadGroup getMaxPriority()用法及代碼示例
- Java ThreadGroup getParent()用法及代碼示例
- Java ThreadGroup getName()用法及代碼示例
- Java ThreadGroup parentOf()用法及代碼示例
- Java ThreadGroup interrupt()用法及代碼示例
- Java ThreadGroup suspend()用法及代碼示例
- Java ThreadGroup setDaemon()用法及代碼示例
- Java ThreadGroup isDaemon()用法及代碼示例
- Java ThreadGroup checkAccess()用法及代碼示例
- Java ThreadGroup resume()用法及代碼示例
- Java ThreadGroup isDestroyed()用法及代碼示例
- Java ThreadGroup toString()用法及代碼示例
- Java ThreadGroup destroy()用法及代碼示例
- Java ThreadGroup list()用法及代碼示例
- Java ThreadGroup stop()用法及代碼示例
- Java ThreadGroup setMaxPriority()用法及代碼示例
- Java Thread toString()用法及代碼示例
注:本文由純淨天空篩選整理自 Java ThreadGroup activeCount() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。