improve get pid logic

This commit is contained in:
hengyunabc 2018-09-30 15:20:41 +08:00
parent fdbe1a48ea
commit 86d1d4bfb7
2 changed files with 33 additions and 2 deletions

View File

@ -0,0 +1,32 @@
package com.taobao.arthas.core.util;
import java.lang.management.ManagementFactory;
/**
*
* @author hengyunabc 2018-09-30
*
*/
public class ApplicationUtils {
private static String PID = "-1";
static {
// https://stackoverflow.com/a/7690178
String jvmName = ManagementFactory.getRuntimeMXBean().getName();
int index = jvmName.indexOf('@');
if (index > 0) {
try {
PID = Long.toString(Long.parseLong(jvmName.substring(0, index)));
} catch (Throwable e) {
// ignore
}
}
}
public static String getPid() {
return PID;
}
}

View File

@ -1,7 +1,6 @@
package com.taobao.arthas.core.util;
import java.io.File;
import java.lang.management.ManagementFactory;
import com.taobao.arthas.core.view.Ansi;
@ -46,7 +45,7 @@ public interface Constants {
/**
* 当前进程PID
*/
String PID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
String PID = ApplicationUtils.getPid();
/**
* 缓存目录