[英]Generate thread dump from java code
I have the following method which overrides the default sorter in Jenkins: 我有以下方法可以覆盖Jenkins中的默认排序器:
@Override
public void sortBuildableItems(List<Queue.BuildableItem> items) {
logBuildQueue(items, "BEFORE");
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future future = executorService.submit(new Runnable() {
@Override public void run() {
items.sort(CustomSorter::compare);
}
});
try {
future.get(2, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
LOGGER.log(Level.SEVERE, "[INTERRUPTED EXCEPTION] Message:" + e.getMessage() + " StackTrace: " + Arrays.toString(e.getStackTrace()));
setDefaultSorter();
e.printStackTrace();
}
catch (ExecutionException e) {
LOGGER.log(Level.SEVERE, "[EXECUTION EXCEPTION] Message:" + e.getMessage() + " StackTrace: " + Arrays.toString(e.getStackTrace()));
setDefaultSorter();
e.printStackTrace();
}
catch (TimeoutException e) {
LOGGER.log(Level.SEVERE, "[TIMEOUT EXCEPTION] Sorting the items took more then 2 seconds, Message:" + e.getMessage() + " StackTrace: " + Arrays.toString(e.getStackTrace()));
setDefaultSorter();
e.printStackTrace();
}
logBuildQueue(items, "AFTER");
}
I'm looking to print all I can get my hands on in case an exception occurs, and I was wondering if there's any way to get a thread dump pushed to the logs, since the error message might be absent. 我正在尝试打印所有可以得到的信息,以防万一发生异常,并且我想知道是否有任何方法可以将线程转储推送到日志中,因为可能不存在错误消息。
public void dumpThreadDump() {
ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
for (ThreadInfo ti : threadMxBean.dumpAllThreads(true, true)) {
System.out.print(ti.toString());
}
}
This seems to be a nice way of programatically obtaining a thread dump from the current application. 这似乎是从当前应用程序以编程方式获取线程转储的一种好方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.