Java 中獲取當前程序的進程
Java 中獲取進程 ID 的方法如下, 注意是進程, 不是線程
public static void main(String[] args) {
System.out.println("==== APP STARTED ====");
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
String name = runtime.getName();
System.out.println(name);
System.out.println("Process ID: " + name.substring(0, name.indexOf("@")));
}
獲取線程ID的方法:
Thread thread = Thread.currentThread();
System.out.println("Thread ID: " + thread.getId());
- 上一篇 ?Java線程詳解(深度好文)
- 下一篇 ?Java獲取Process子進程進程ID方法詳解