import java.lang.management.ManagementFactory;
/**
* Utility class to get the current process ID
*/
public class ProcessIDUtil
{
public static void main ( String [] args )
{
long currentPID = getCurrentPID ();
System.out.println ( "Current Process ID is " + currentPID );
}
/**
* This method returns the current process ID
*
* @return process ID
*/
public static long getCurrentPID ()
{
// The name is normally of the format <PID>@<Machine-Name>. For eg: 1416@htipl-50291
String name = ManagementFactory.getRuntimeMXBean ().getName ();
long pid = Long.parseLong ( name.split ( "@" )[0] );
return pid;
}
}
得到当前进程的ID
最新推荐文章于 2021-11-14 14:27:18 发布