How do I get peak live threads count?

package org.kodejava.example.lang.management;

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

public class PeakThreadCount {
    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 peak live thread count since the Java virtual
        // machine started or peak was reset.
        //
        int peakThreadCount = bean.getPeakThreadCount();
        System.out.println("Peak Thread Count = " + peakThreadCount);
    }
}

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);
    }
}

How do I get the Java classpath?

The RuntimeMXBean.getClassPath() returns the Java class path that is used by the system class loader to search for class files. This method is equivalent to System.getProperty("java.class.path").

Multiple paths in the Java class path are separated by the path separator character of the platform of the Java virtual machine being monitored.

package org.kodejava.example.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

public class GetClassPath {
    public static void main(String[] args) {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
        String classPath = bean.getClassPath();
        System.out.println("classPath = " + classPath);
    }
}

How do I get system properties information?

package org.kodejava.example.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Map;
import java.util.Set;

public class GetSystemProperties {
    public static void main(String[] args) {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();

        //
        // Returns a map of names and values of all system
        // properties. This method calls System.getProperties()
        // to get all system properties. Properties whose
        // name or value is not a String are omitted.
        //
        Map systemProperties = bean.getSystemProperties();
        Set keys = systemProperties.keySet();
        for (String key : keys) {
            String value = systemProperties.get(key);
            System.out.printf("Property[%s] = %s.n", key, value);
        }
    }
}

How do I get the start time of a JVM?

package org.kodejava.example.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Date;

public class GetStartTime {
    public static void main(String[] args) {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();

        //
        // Returns the start time of the Java virtual machine in 
        // milliseconds. This method returns the approximate time 
        // when the Java virtual machine started.
        //
        long startTime = bean.getStartTime();
        Date startDate = new Date(startTime);        
        System.out.println("Start Time = " + startTime);
        System.out.println("Start Date = " + startDate);
    }
}

How do I get the up time of a JVM?

package org.kodejava.example.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

public class GetUpTime {
    public static void main(String[] args) {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();

        //
        // Returns the up time of the Java virtual machine in
        // milliseconds.
        //
        long upTime = bean.getUptime();
        System.out.printf("Up Time = %d (ms)", upTime);
    }
}

How do I get process id of a Java application?

The code below show you how to get the process id of a Java application. We can use the ManagementFactory.getRuntimeMXBean().getName() to get the process id. In Windows the method return a string in the form of [PID]@[MACHINE_NAME].

package org.kodejava.example.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

public class GetProcessID {
    public static void main(String[] args) {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();

        //
        // Get name representing the running Java virtual machine.
        // It returns something like [email protected] Where the value
        // before the @ symbol is the PID.
        //
        String jvmName = bean.getName();
        System.out.println("Name = " + jvmName);

        //
        // Extract the PID by splitting the string returned by the
        // bean.getName() method.
        //
        long pid = Long.valueOf(jvmName.split("@")[0]);
        System.out.println("PID  = " + pid);
    }
}

Here is the result of the code above:

Name = [email protected]
PID  = 8564