将异常从一个线程重新抛出到另一个线程

Rethrowing exception from one thread to another

我有一个场景,我希望一个线程执行一些循环操作,第二个(主)线程执行一些其他循环工作,而第一个线程仍在执行其工作。

我的想法是使用 CountDownLatch 并等待直到它在主线程中完成:

public void process() {

    CountDownLatch countDownLatch = new CountDownLatch(10_000);
    Future<?> future = Executors.newSingleThreadExecutor().submit(() -> {
        for (int i = 0; i < 10_000; i++) {
            // do some stuff
            countDownLatch.countDown();
        }
    });

    try {
        while (!countDownLatch.await(5, SECONDS)) {
            // do some other stuff...
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } 
}

问题是有时会在第一个(未来的)线程中抛出异常,在这种情况下继续在主线程中执行代码是没有意义的。

我正在考虑将此类异常(从第一个线程抛出)的引用分配给 volatile 字段,并在 main 的线程循环中对该字段进行空检查以查看它是否应该继续循环:

private volatile Exception innerException;

public void process() {

    CountDownLatch countDownLatch = new CountDownLatch(10_000);
    Future<?> future = Executors.newSingleThreadExecutor().submit(() -> {
        try {
            for (int i = 0; i < 10_000; i++) {
                // do some stuff
                countDownLatch.countDown();
            }
        } catch (Exception e) {
            this.innerException = e;
            throw e;
        }
    });

    try {
        while (!countDownLatch.await(1, SECONDS)) {
            // do some other stuff... but it doesn't make sense to continue
            // if 'future' has thrown an exception, so let's rethrow it:
            if (innerException != null) {
                throw innerException;
            }
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (Exception e) {
        log.error("Something bad happened in the 'future'! : ", e);
    }
}

我想知道这是否是一个好的(安全的?)想法,或者是否有更好的方法来解决此类问题?

感谢任何关于这方面的帮助,谢谢!

您可以使用 future.get 同步未来的完成。如果 Runnable/Callable 抛出异常,则 future.get 将抛出 ExecutionException。您可以完全摆脱 CountDownLatch。