2015-05-23 144 views
1

的文檔ThreadGroup#activeCount()說:返回此線程組及其子組中活動線程數的估計值。
是否包含線程睡眠,等待並加入模式或只有那些正在執行的線程運行方法?關於ThreadGroup的混淆#activeCount()

謝謝。

回答

2

您可以輕鬆地嘗試這個辦法:

Thread t1 = new Thread(new Runnable() { 

    @Override 
    public void run() { 
     Scanner sc = new Scanner(System.in); 
     sc.nextInt(); 
    } 
}); 
Thread t2 = new Thread(new Runnable() { 

    @Override 
    public void run() { 
     try { 
      Thread.sleep(10000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
}); 
t1.start(); // this will be RUNNABLE 
t2.start(); // this will be TIMED_WAITING 
System.out.println(Thread.currentThread().getThreadGroup().activeCount()); 

打印3.在談到線條

t1.start(); 
t2.start(); 

導致打印1.