In this article we will see how to find all the active threads running. One can find all the active threads in a thread group and its sub-groups using enumerate()
. See below:
Thread[] threads = new Thread[32]; int count = threadGroup.enumerate(threads);
The count
is the number of threads put into the array. If they are same as the array length then there is a possibility of more active threads in which case we would like to double the array and call enumerate()
again so we don’t miss out on any active threads.
Thread[] threads = new Thread[32]; int count = threadGroup.enumerate(threads); while (count == threads.length) { threads = new Thread[threads.length * 2]; count = threadGroup.enumerate(threads); }
If we can get the root thread group then we can find all the active threads in the JVM. The root thread group is the top most group with no parent.
ThreadGroup candidate = Thread.currentThread().getThreadGroup(); while (candidate.getParent() != null) { candidate = candidate.getParent(); } return candidate;
We create some threads and group them into two groups, high priority and low priority. In the end we print the active threads.
FindAllThreadsExample:
package com.javarticles.threads; import java.util.Arrays; public class FindAllThreadsExample { public static void main(String[] args) { ThreadGroup highPriorityGroup = new ThreadGroup("High Priority"); Thread someTaskThread1 = new Thread(highPriorityGroup, new SomeTask(), "Some task High Priority thread1"); Thread someTaskThread2 = new Thread(highPriorityGroup, new SomeTask(), "Some task High Priority thread2"); someTaskThread1.start(); someTaskThread2.start(); ThreadGroup lowPriorityGroup = new ThreadGroup("Low Priority"); Thread someTaskThread3 = new Thread(lowPriorityGroup, new SomeTask(), "Some task Low Priority thread1"); someTaskThread3.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Arrays.asList(ThreadUtils.getAllThreads())); } private static class SomeTask implements Runnable { @Override public void run() { System.out.println("Running " + Thread.currentThread().getName() + "..."); try { Thread.sleep(100000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Some task done"); } } }
Below utility class contains the methods to find the root thread group and the active threads.
ThreadUtils:
package com.javarticles.threads; import java.util.Arrays; public class ThreadUtils { public static Thread[] getAllThreads() { ThreadGroup rootThreadGroup = getRootThreadGroup(); Thread[] threads = new Thread[32]; int count = rootThreadGroup.enumerate(threads); while (count == threads.length) { threads = new Thread[threads.length * 2]; count = rootThreadGroup.enumerate(threads); } return Arrays.copyOf(threads, count); } private static ThreadGroup getRootThreadGroup() { ThreadGroup candidate = Thread.currentThread().getThreadGroup(); while (candidate.getParent() != null) { candidate = candidate.getParent(); } return candidate; } }
The active threads contain system define threads as well as the threads we created.
Output:
Running HighPriorityThread1... Some task done Running LowPriorityThread3... Running LowPriorityThread2... Running HighPriorityThread3... Running HighPriorityThread2... Running LowPriorityThread1... Some task done Some task done [Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[Signal Dispatcher,9,system], Thread[Attach Listener,5,system], Thread[main,5,main], Thread[HighPriorityThread3,5,High Priority], Thread[LowPriorityThread2,5,Low Priority], Thread[LowPriorityThread3,5,Low Priority]] Some task done Some task done Some task done
Download the source code
This was an example about finding active threads in JVM.