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)
我想知道这是否是一个好(安全?)的想法,或者也许有一些更好的方法来解决此类问题?
感谢对此的任何帮助,谢谢!
您可以使用 future.get 同步 future 的完成情况。如果 Runnable/Callable 抛出异常,future.get 将抛出 ExecutionException。您可以完全摆脱 CountDownLatch。
| 归档时间: |
|
| 查看次数: |
168 次 |
| 最近记录: |