Java 8並行流如何在消費子句中拋出的異常上行爲,例如在forEach
處理中?例如,以下代碼:Java 8並行流如何在拋出的異常上運行?
final AtomicBoolean throwException = new AtomicBoolean(true);
IntStream.range(0, 1000)
.parallel()
.forEach(i -> {
// Throw only on one of the threads.
if (throwException.compareAndSet(true, false)) {
throw new RuntimeException("One of the tasks threw an exception. Index: " + i);
});
是否立即停止處理的元素?它是否等待已經啓動的元素完成?它是否等待所有的流完成?拋出異常後它是否開始處理流元素?
它什麼時候返回?異常之後立即?畢竟/部分元素是由消費者處理的?
在並行流拋出異常後繼續處理元素嗎? (發現這種情況發生)。
這裏有一個通用規則嗎?
編輯(15-11-2016)
以確定是否可以並行流早回來,我發現它不確定性:
@Test
public void testParallelStreamWithException() {
AtomicInteger overallCount = new AtomicInteger(0);
AtomicInteger afterExceptionCount = new AtomicInteger(0);
AtomicBoolean throwException = new AtomicBoolean(true);
try {
IntStream.range(0, 1000)
.parallel()
.forEach(i -> {
overallCount.incrementAndGet();
afterExceptionCount.incrementAndGet();
try {
System.out.println(i + " Sleeping...");
Thread.sleep(1000);
System.out.println(i + " After Sleeping.");
}
catch (InterruptedException e) {
e.printStackTrace();
}
// Throw only on one of the threads and not on main thread.
if (!Thread.currentThread().getName().equals("main") && throwException.compareAndSet(true, false)) {
System.out.println("Throwing exception - " + i);
throw new RuntimeException("One of the tasks threw an exception. Index: " + i);
}
});
Assert.fail("Should not get here.");
}
catch (Exception e) {
System.out.println("Cought Exception. Resetting the afterExceptionCount to zero - 0.");
afterExceptionCount.set(0);
}
System.out.println("Overall count: " + overallCount.get());
System.out.println("After exception count: " + afterExceptionCount.get());
}
逾期歸還從拋時不主線程。這導致很多新的元素在拋出異常後被處理。在我的機器上,拋出異常後處理了大約200個元素。但是,並非所有的1000個元素都被處理了。那麼這裏的規則是什麼?爲什麼即使拋出異常也處理了更多元素?
提前返回時刪除不是(!
)符號,導致異常在主線程中拋出。只有已經開始的元素完成了處理,並沒有處理新的元素。早在這裏就是這種情況。與以前的行爲不一致。
我在這裏錯過了什麼?
你可以根據一些文件? –
@ AlikElzin-kilaka不是真的,我不認爲這是記錄。我記得通過閱讀其他SO問題,引用了這個bug:https://bugs.openjdk.java.net/browse/JDK-8164690 – Eugene
@ AlikElzin-kilaka還有[this](http:// mail.openjdk.java.net/pipermail/core-libs-dev/2016-August/042972.html)在core-libs-dev郵件列表上討論的話題,這個郵件列表導致了Eugene提到的JBS bug。 –