Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

How to get Process ID and Live Threads of a Java Application?

$
0
0

This is a simple Java Thread Life Cycle diagram for your reference.

Java Thread Lifecycle - Crunchify Tips

Below is a simple program which tells you how to get Process ID and Total # of Live Threads of a Java Application.

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

The post How to get Process ID and Live Threads of a Java Application? appeared first on Crunchify.


Viewing all articles
Browse latest Browse all 1037

Trending Articles