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

t3h*_*b0t 2 java concurrency multithreading exception

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

我的想法是使用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();
    } 
}
Run Code Online (Sandbox Code Playgroud)

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

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

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);
    }
}

Run Code Online (Sandbox Code Playgroud)

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

感谢对此的任何帮助,谢谢!

pve*_*jer 6

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

  • future 有一个“isDone”方法,可用于检查 future 是否准备好。我不确定在某个循环中检查 isDone 是否是最好的方法;这将取决于具体情况。 (2认同)
  • 或者使用 `CompletableFuture&lt;?&gt; future = CompletableFuture.runAsync(() -&gt; { /* your operation */ }, yourExecutor);`,那么,您不仅可以使用 `isDone()` 来检查一般情况下的完成情况,但也可以重新抛出“if(future.isCompletedExceptionally()) future.join();”。 (2认同)