ArrayList
是
不
一个线程安全的集合,并尝试使用
parallelStream
,你看到的是
resizing
它的内部数组
阵列列表
-该操作也不是线程安全的,首先要增加数组并
然后
将元素移动到其中:可能是元素尚未移动,您尝试访问它。
为了使其更明确,当调整大小时
创建的数组的大小增加了-如果满了
null
s
只有
编辑
可见:
List<Integer> result = IntStream.iterate(0, i -> i + 1)
.takeWhile(i -> {
String arr[] = { "A", "B", "C", "D", "E", "F", "G", "H", "R", "T" };
List<String> list = new ArrayList<>();
Arrays.stream(arr)
.parallel()
.peek(list::add)
.forEach(x -> {
});
if (list.contains(null)) {
System.out.println("It took " + i + " steps to see a null");
return false;
}
return true;
})
.boxed()
.collect(Collectors.toList());