Java Concurrency - Process (Main thread)

1 - About

process in Java

A process in Java is:

  • started by a main class.
  • implemented as a main thread that can create other thread.

It has:

  • a self-contained execution environment.
  • a complete, private set of basic run-time resources; in particular, each process has its own memory space.

3 - Management

3.1 - Creation

Most implementations of the Java virtual machine run as a single process. A Java application can create additional processes using a ProcessBuilder object.

3.2 - List

The jps command will report the local VM identifier, or lvmid for each instrumented JVM found on the target system (The lvmid is generally the operating system's process identifier).

More Java - jps (Java Process Utility)

3.3 - Get Pid

3.3.1 - From Jps

with Java - jps (Java Process Utility)

jps | grep -i name | awk '{print $1}'

3.3.2 - From Java

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public static int getCurrentPID() {
    try{ runtime = 
        java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm");
        jvm.setAccessible(true); mgmt = 
           ( jvm.get(runtime);
        java.lang.reflect.Method pid_method = 
        return (Integer) pid_method.invoke(mgmt);
    catch(Exception e){
        System.out.println("Failed at getting the process ID");

3.4 - Name - Set process name for Java on Linux

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap