我有一个场景,我希望一个线程执行一些循环操作,第二个(主)线程执行一些其他循环工作,而第一个线程仍在执行其工作。
我的想法是使用 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();
}
}
问题是有时会在第一个( future 的)线程中抛出异常,在这种情况下继续在主线程中执行代码是没有意义的。
我正在考虑将此类异常(从第一个线程抛出)的引用分配给 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 同步 future 的完成。如果 Runnable/Callable 抛出异常,future.get 将抛出 ExecutionException。您可以完全摆脱 CountDownLatch。
关于java - 将异常从一个线程重新抛出到另一个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70205874/