如何找到调用threadPool

时间:2019-04-16 08:57:48

标签: java stack-trace

我在生产应用程序中存在一些错误,但找不到原因。我尝试获取一些日志以找到一种方法,该方法调用了我的method()。但是因为我使用threadPool,所以我不能只获取Thread.currentThread().getStackTrace()并遍历StackTraceElements,所以它仅在ThreadPool之前显示一些行。 如果我使用下一个代码,我将获得所需的每种方法,但它是如此的广泛。在我的测试环境中,一个文本文件中只有1次调用方法花费400+ Kb。我认为,在生产中,每秒将达到约1 Mb。

private final ExecutorService completableFutureExecutor =
            new ThreadPoolExecutor(10, 2000, 60L, TimeUnit.SECONDS, new SynchronousQueue<>());

public void firstMethod(){

    secondMethod();    
}

private CompletableFuture<Void> secondMethod(){

    return CompletableFuture.supplyAsync(()->method(),threadPool);

}

void method(){
    Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
        for (Thread thread : map.keySet()) {
            printLog(thread);
        }
}
private void printLog(Thread thread) {
        StringBuilder builder = new StringBuilder();
        for (StackTraceElement s : thread.getStackTrace()) {
            builder.append("\n getClass = " + s.getClass());
            builder.append("\n getClassName = " + s.getClassName());
            builder.append("\n getFileName = " + s.getFileName());
            builder.append("\n getLineNumber = " + s.getLineNumber());
            builder.append("\n getMethodName = " + s.getMethodName());
            builder.append("\n  ---------------------------- \n ");
        }
        ownLogger.info("SomeThread = {} ", builder);
    }

如何找到呼叫firstMethod()的{​​{1}}吗?

2 个答案:

答案 0 :(得分:0)

由于我没有找到任何好的解决方案,我自己就是在CompletableFuture调用之前和之后放置logger

看起来

Logger beforeAsync= LoggerFactory.getLogger("beforeAsync");
Logger afterAsync= LoggerFactory.getLogger("afterAsync");

private CompletableFuture<Void> secondMethod(){
    printLongerTrace(Thread.currentThread(),beforeAsync);
    return CompletableFuture.supplyAsync(()->method(),threadPool);
}

private void methodWithException(){
    try{
    //do something
    }
    catch(Exception e){ 
      printLongerTrace(e,"methodWithException", afterAsync);
    }
}

public void printLongerTrace(Throwable t, String methodName, Logger ownlogger) {

    if (t.getCause() != null) {
        printLongerTrace(t.getCause(), methodName, fields, ownlogger);
    }
    StringBuilder builder = new StringBuilder();
    builder.append("\n Thread = " + Thread.currentThread().getName());
    builder.append("ERROR CAUSE = " + t.getCause() + "\n");
    builder.append("ERROR MESSAGE = " + t.getMessage() + "\n");

    printLog(t.getStackTrace(), builder);
    ownlogger.info(methodName + "Trace ----- {}", builder);
}

public void printLongerTrace(Thread t, Logger ownlogger) {

    StringBuilder builder = new StringBuilder();
    builder.append("\n Thread = " + Thread.currentThread().getName());

    printLog(t.getStackTrace(), builder);
    ownlogger.info("Trace ----- {}", builder);
}

private StringBuilder printLog(StackTraceElement[] elements, StringBuilder builder) {

    int size = elements.length > 15 ? 15 : elements.length;

    for (int i = 0; i < size; i++) {
        builder.append("Line " + i + " = " + elements[i] + "   with method = " + elements[i].getMethodName() + "\n");
    }
    return builder;
}

printLongerTrace(Throwable t, String methodName, Logger ownlogger)需要打印异常的所有原因。

printLongerTrace(Thread t, Logger ownlogger)需要在CompletableFuture之前打印哪个方法调用

答案 1 :(得分:-1)

只需通过调用Thread.dumpStack()来转储堆栈,但这仅用于调试并且开销很大,因为转储堆栈会占用大量CPU