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());