是的你可以。我一直在成功地使用此代码来调试随机触发的并发错误:
package utils.stack;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.management.ManagementFactory;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.function.Supplier;
import javax.management.JMX;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
public interface DiagnosticCommand {
String threadPrint(String... args);
DiagnosticCommand local = ((Supplier<DiagnosticCommand>) () -> {
try {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("com.sun.management",
"type", "DiagnosticCommand");
return JMX.newMBeanProxy(server, name, DiagnosticCommand.class);
} catch(MalformedObjectNameException e) {
throw new AssertionError(e);
}
}).get();
static void dump() {
String print = local.threadPrint();
Path path = Paths.get(LocalDateTime.now() + ".dump.txt");
try {
byte[] bytes = print.getBytes("ASCII");
Files.write(path, bytes);
} catch(IOException e) {
throw new UncheckedIOException(e);
}
}
}
它需要 Java 8 和 HotSpot 作为 JVM,因为它模仿 jstack 正在做的事情,除了在同一个进程中。