简体   繁体   中英

How to show stack trace in uncaught java exception message when using forkJoinPool

When I have an uncaught exception in an instance of a recursiveTask (from forkJoinPool ). the JVM prints a single line of error message telling me the type of the exception,

XXX.XXX.XXXXXXException

but without stack trace. With some testing, I realized that when there is only one instance of recursiveTask present in the forkJoinPool (ie by setting the sThreshold from the example in the link sufficiently large), the stack trace is always printed; with multiple instances present, there is no stack trace. The question is, how can I always get the stack trace?

Follow-up:

an attempt to use the third constructor for forkJoinPool

private static final ForkJoinPool threadPool = 
new ForkJoinPool(Runtime.getRuntime().availableProcessors(), 
defaultForkJoinWorkerThreadFactory, 
new ExceptionHandler(), false);

The ExceptionHandler class:

final class ExceptionHandler implements Thread.UncaughtExceptionHandler {

    ExceptionHandler() {
    }

    @Override
    public void uncaughtException(Thread t, Throwable err) {
        System.err.println("Uncaught " + err + " in thread " + t);
        err.printStackTrace();
    }
}

The pool is invoked.

threadPool.invoke(new aForkJoinTask());
...
threadPool.invoke(new differentForkJoinTask());

Somewhere in the RecursiveAction I put in

int i = Integer.parseInt("word");

However, solely the following is printed as an error message

java.lang.NumberFormatException

Meanwhile the same uncaught exception in the main thread prints a stack trace.

Though I have not tried but IMO you can use the following constructor :

ForkJoinPool(int parallelism, ForkJoinPool.ForkJoinWorkerThreadFactory factory, Thread.UncaughtExceptionHandler handler, boolean asyncMode)

It provides an option to declare the handler for internal worker threads that terminate due to unrecoverable errors encountered while executing tasks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM