public
static int activeCount()
Returns the number of active threads in the current thread's thread group.
Returns the number of active threads in the current thread's thread group.
class ThreadCountEx implements Runnable{ public void run(){ try{ Thread.currentThread().sleep(1000); } catch(InterruptedException e){ } } public static void main(String args[])throws Exception{ ThreadCountEx task1 = new ThreadCountEx(); ThreadCountEx task2 = new ThreadCountEx(); Thread t1 = new Thread(task1); Thread t2 = new Thread(task2); t1.start(); t2.start(); System.out.println("Active thread count is " + t1.activeCount()); t1.join(); t2.join(); System.out.println("Active thread count is " + t1.activeCount()); } }
Output
Active thread count is 3 Active thread count is 1
As
you observe the output, threads t1 and t2 are created in main thread,
main thread is part of main thread group. So threads t1 and t2 also
are part of main thread group. ActiveCount() returns 3 since 2
threads t1 and t2, main thread are active in the main thread group. Since activeCount() is static method, you can directly call it by using class name.
No comments:
Post a Comment