Java Thread.activeCount() Method Last Updated : 24 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Thread.activeCount() method is a static method of the Thread class in Java that returns the number of active threads in the current thread's thread group and its subgroups. It provides an estimate of how many threads are currently active and being executed in the program, including threads from any subgroups.Example 1: Below is an example that demonstrates the usage of the Thread.activeCount() method. Java // Java program to demonstrate the // Thread.activeCount() method public class Geeks { public static void main(String[] args) { // Create and start threads Thread t1 = new Thread(() -> { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } }); t1.start(); Thread t2 = new Thread(() -> { try { Thread.sleep(150); } catch (InterruptedException e) { e.printStackTrace(); } }); t2.start(); // Get the number of active threads int activeThreads = Thread.activeCount(); // Print the number of active threads System.out.println("Number of active threads: " + activeThreads); } } OutputNumber of active threads: 3 Explanation:In the above example, we created some example threads and checked the currently active threads using the activeCount() method.Every Java program starts with a main thread. This thread is responsible for executing the code within the main method that's why the count is 3.The activeCount() method gives the estimation of active threads.It is used to monitor the different states of threads in a Multithreaded environment.Syntax of Thread.activeCount() Methodpublic static int activeCount(); Return Type: It returns the number of threads that are currently active in the thread group of the current thread.Behavior:Thread Group: The method counts all active threads in the current thread's thread group and any subgroups of that group.Dynamic Nature: The count is an estimate since thread states can change dynamically (e.g., threads starting or finishing).Example 2: In this example, we will use the Thread.activeCount() to check the number of active threads in a thread pool environment. Java // Java Program to check the number of // active threads in a thread pool environment import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; class Geeks extends Thread { private String v; public Geeks(String v) { this.v = v; } public void run() { try { System.out.println(v + " is starting."); Thread.sleep(200); System.out.println(v + " has finished."); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { // Create a thread pool with 2 threads ExecutorService executor = Executors.newFixedThreadPool(2); // Print the initial thread count System.out.println("Initial: " + Thread.activeCount()); // Submit tasks to the thread pool executor.submit(new Geeks("Task 1")); executor.submit(new Geeks("Task 2")); // Shut down the executor service immediately executor.shutdown(); // Print the final thread count // after tasks complete System.out.println("Final: " + Thread.activeCount()); } } OutputInitial: 1 Final: 3 Task 2 is starting. Task 1 is starting. Task 2 has finished. Task 1 has finished. Explanation: The above program first creates a thread pool with two threads and prints the initial thread count. Then, two tasks are submitted to the executor, and the executor is immediately shut down. Then it prints the final thread count, effectively showing the change in active threads before and after task execution. The returns an estimate of the number of active threads in the current thread's thread group. The main thread is itself a thread and part of the thread group, it is included in the count. Comment More infoAdvertise with us Next Article Java HashSet size() Method D devanshumi853 Follow Improve Article Tags : Java Java-Multithreading Java Thread Class Practice Tags : Java Similar Reads Java Thread.enumerate() Method Thread.enumerate() method in Java is used to retrieve all active threads in the current thread's thread group, and it stores these threads in an array. This method provides a way to inspect the currently running threads in a program. It is a static method in the Thread class, which means it can be c 3 min read Method Class | getParameterCount() method in Java The java.lang.reflect.Method.getParameterCount() method of Method class returns the number of parameters declared on a method Object. Syntax: public int getParameterCount() Return Value: This method returns number of formal parameters defined on this Method Object. Below programs illustrates getPara 3 min read Java HashSet size() Method The HashSet size() method in Java is used to get the size of the HashSet or the number of elements present in the HashSet. Syntax of HashSet size() Methodint size()Return Type: This method returns an integer value that represents the number of elements currently stored in the HashSet.Example: This e 1 min read Stream count() method in Java with examples long count() returns the count of elements in the stream. This is a special case of a reduction (A reduction operation takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation). This is a terminal operation i.e, it may travers 2 min read Java Integer bitCount() Method The bitCount() method of Integer class of java.lang package returns the count of set bits in a positive number. For negative numbers, it returns the count of set bits in its 2s complement form. This function is sometimes referred to as the population count. Syntax:public static int bitCount(int n)Pa 2 min read Java streams counting() method with examples In Java 8, there is predefined counting() method returned by Collectors class counting() method to count the number of elements in a Stream. Syntax : public static Collector counting() Where, output is a Collector, acting on a Stream of elements of type T, with its finisher returning the âcollectedâ 2 min read Like