This is a simple Java Thread Life Cycle diagram for your reference.
Below is a simple program which tells you how to get Process ID and Total # of Live Threads of any Java Application.
We are using ThreadMXBean management interface for the thread system of the Java virtual machine (JVM). Bean contains number of very important methods which we could use at runtime to analyze thread and application behavior.
Here is a complete Java Example
package com.crunchify.tutorials;
/**
* @author Crunchify.com
*/
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
public class CrunchifyGetProcessIDThread {
public static void main(String[] args) {
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
String jvmName = runtimeBean.getName();
System.out.println("JVM Name = " + jvmName);
long pid = Long.valueOf(jvmName.split("@")[0]);
System.out.println("JVM PID = " + pid);
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
int peakThreadCount = bean.getPeakThreadCount();
System.out.println("Peak Thread Count = " + peakThreadCount);
}
}
Output:
Name = 12228@D-DESKTOP PID = 12228 Peak Thread Count = 5


