Java 8并行流如何在使用子句中的抛出异常上运行,例如在forEach处理中?例如,以下代码:
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);
});
Run Code Online (Sandbox Code Playgroud)
它会立即停止处理元素吗?是否等待已经启动的元素完成?是否等待所有流完成?抛出异常后是否开始处理流元素?
什么时候回来?异常后立即?毕竟/部分元素是由消费者处理的?
在并行流引发异常后,是否继续处理元素?(发现这种情况发生的情况).
这里有一般规则吗?
编辑(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 …Run Code Online (Sandbox Code Playgroud) parallel-processing exception-handling exception java-8 java-stream
当我们向流API提供一些输入时,它将其分成块,JVM创建多个线程以在该块上执行执行.
题 :-
如果我给出了数百万个条目ArrayList作为并行管道的输入,并且在List by JVM上的计算的前半部分之后发生内部线程异常.
JVM如何处理ROll Back.
JVM真的回滚到了原始状态吗?