从java代码生成线程转储

时间:2018-06-08 14:11:09

标签: java multithreading jenkins thread-dump

我有以下方法覆盖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");
}

我正在寻找打印所有我可以得到的东西,以防异常发生,我想知道是否有任何方法将线程转储推送到日志,因为错误消息可能不存在。

1 个答案:

答案 0 :(得分:0)

{{1}}

这似乎是一种从当前应用程序以编程方式获取线程转储的好方法。