Printing out active threads in java

Here’s the codeĀ snippetsĀ to display the number of running threads in your java program.

It’s good to use it to track your resources.

int activeCount = Thread.activeCount();
System.out.println("total active = " + activeCount);
Thread[] threads = new Thread[activeCount];
Thread.enumerate(threads);

System.out.println("before");
for (int j=0; j<threads.length; j++) {
 System.out.println(threads[j].toString());
}

//run your classes
Benny benny = new Benny();

activeCount = Thread.activeCount();
System.out.println("total active = " + activeCount);

threads = new Thread[activeCount];
System.out.println("after");
Thread.enumerate(threads);
for (int i=0; i<threads.length; i++) {
 System.out.println(threads[i].toString());
}

Similar Posts:

VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
Printing out active threads in java, 10.0 out of 10 based on 1 rating