How do I get current number of live threads?

package org.kodejava.example.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

public class ThreadCount {

    public static void main(String[] args) {
        //
        // Get the managed bean for the thread system of the Java
        // virtual machine.
        //
        ThreadMXBean bean = ManagementFactory.getThreadMXBean();

        //
        // Get the current number of live threads including both
        // daemon and non-daemon threads.
        //
        int threadCount = bean.getThreadCount();        
        System.out.println("Thread Count = " + threadCount);
    }
}
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply