About parallelStream collect results. Please Use the collect method. The collect method is concurrent secure.
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import org.junit.Test;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ParallelStreamTestcase {
@Test
public void test0Error() {
int total = 100000;
List<Integer> source = new ArrayList<>(total);
for (int i = 0; i < total; i++) {
source.add(i);
}
List<Integer> target = new ArrayList<>();
source.parallelStream().forEach(iterator -> target.add(iterator));
assertEquals(total, source.size());
assertEquals(total, target.size());
}
@Test
public void test1Error() {
int total = 100000;
List<Integer> source = new ArrayList<>(total);
for (int i = 0; i < total; i++) {
source.add(i);
}
HashSet<Integer> target = new HashSet<>();
source.parallelStream().forEach(iterator -> target.add(iterator));
assertEquals(total, source.size());
assertEquals(total, target.size());
}
@Test
public void test2Error() {
int total = 100000;
List<Integer> source = new ArrayList<>(total);
for (int i = 0; i < total; i++) {
source.add(i);
}
HashMap<Integer, Integer> target = new HashMap<>();
source.parallelStream().forEach(iterator -> target.put(iterator, iterator));
assertEquals(total, source.size());
assertEquals(total, target.size());
}
@Test
public void test00() {
int total = 100000;
List<Integer> source = new ArrayList<>(total);
for (int i = 0; i < total; i++) {
source.add(i);
}
List<Integer> targetList = source.parallelStream().collect(Collectors.toList());
assertEquals(total, source.size());
assertEquals(total, targetList.size());
Set<Integer> targetSet = source.parallelStream().collect(Collectors.toSet());
assertEquals(total, targetSet.size());
Set<Integer> targetTreeSet = source.parallelStream().collect(Collectors.toCollection(() -> new TreeSet<>()));
assertEquals(total, targetTreeSet.size());
LinkedList<Integer> targetLinkedList = source.parallelStream().collect(Collectors.toCollection(() -> new LinkedList<>()));
assertEquals(total, targetLinkedList.size());
}
@Test
public void test22() {
int total = 100000;
List<Integer> source = new ArrayList<>(total);
long sum = 0;
for (int i = 0; i < total; i++) {
sum += i;
source.add(i);
}
log.debug("sum:{}", sum);
System.err.println(String.format("sum:{%d}", sum));
Map<Integer, Integer> target = source.parallelStream().collect(Collectors.toMap(iterator -> iterator, iterator -> iterator));
long targetSum = 0;
for (Integer value : target.values()) {
targetSum += value;
}
log.debug("targetSum:{}", targetSum);
System.err.println(String.format("targetSum:{%d}", targetSum));
assertEquals(sum, targetSum);
assertEquals(total, source.size());
assertEquals(total, target.size());
Map<Object, Integer> target2 = source.parallelStream().collect(Collectors.toMap(iterator -> iterator % 1000, iterator -> iterator, (t, u) -> t + u));
long target2Sum = 0;
for (Integer value : target2.values()) {
target2Sum += value;
}
log.debug("target2Sum:{}", target2Sum);
System.err.println(String.format("target2Sum:{%d}", target2Sum));
assertEquals(sum, target2Sum);
TreeMap<Object, Integer> target3 = source.parallelStream().collect(Collectors.toMap(iterator -> iterator % 1000, iterator -> iterator, (t, u) -> t + u, TreeMap::new));
long target3Sum = 0;
for (Integer value : target3.values()) {
target3Sum += value;
}
log.debug("target3Sum:{}", target3Sum);
System.err.println(String.format("target3Sum:{%d}", target3Sum));
assertEquals(sum, target3Sum);
boolean duplicateKeyExceptionForParallelStream = false;
try {
source.parallelStream().collect(Collectors.toMap(iterator -> iterator % 1000, iterator -> iterator));
} catch (IllegalStateException e) {
duplicateKeyExceptionForParallelStream = true;
}
assertTrue(duplicateKeyExceptionForParallelStream);
boolean duplicateKeyExceptionForStream = false;
try {
source.parallelStream().collect(Collectors.toMap(iterator -> iterator % 1000, iterator -> iterator));
} catch (IllegalStateException e) {
duplicateKeyExceptionForStream = true;
}
assertTrue(duplicateKeyExceptionForStream);
}
@Test
public void test222() {
int total = 100000;
List<Integer> source = new ArrayList<>(total);
long sum = 0;
for (int i = 0; i < total; i++) {
sum += i;
source.add(i);
}
log.debug("sum:{}", sum);
System.err.println(String.format("sum:{%d}", sum));
Map<Integer, Integer> target = source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator, iterator -> iterator));
long targetSum = 0;
for (Integer value : target.values()) {
targetSum += value;
}
log.debug("targetSum:{}", targetSum);
System.err.println(String.format("targetSum:{%d}", targetSum));
assertEquals(sum, targetSum);
assertEquals(total, source.size());
assertEquals(total, target.size());
Map<Object, Integer> target2 = source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator, (t, u) -> t + u));
long target2Sum = 0;
for (Integer value : target2.values()) {
target2Sum += value;
}
log.debug("target2Sum:{}", target2Sum);
System.err.println(String.format("target2Sum:{%d}", target2Sum));
assertEquals(sum, target2Sum);
ConcurrentMap<Object, Integer> target3 = source.parallelStream()
.collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator, (t, u) -> t + u, ConcurrentHashMap::new));
long target3Sum = 0;
for (Integer value : target3.values()) {
target3Sum += value;
}
log.debug("target3Sum:{}", target3Sum);
System.err.println(String.format("target3Sum:{%d}", target3Sum));
assertEquals(sum, target3Sum);
boolean duplicateKeyExceptionForParallelStream = false;
try {
source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator));
} catch (IllegalStateException e) {
duplicateKeyExceptionForParallelStream = true;
}
assertTrue(duplicateKeyExceptionForParallelStream);
boolean duplicateKeyExceptionForStream = false;
try {
source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator));
} catch (IllegalStateException e) {
duplicateKeyExceptionForStream = true;
}
assertTrue(duplicateKeyExceptionForStream);
}
@Test
public void test2222() {
int total = 100000;
List<Integer> source = new ArrayList<>(total);
long sum = 0;
for (int i = 0; i < total; i++) {
sum += i;
source.add(i);
}
log.debug("sum:{}", sum);
System.err.println(String.format("sum:{%d}", sum));
Map<Integer, Integer> target = source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator, iterator -> iterator));
long targetSum = 0;
for (Integer value : target.values()) {
targetSum += value;
}
log.debug("targetSum:{}", targetSum);
System.err.println(String.format("targetSum:{%d}", targetSum));
assertEquals(total, source.size());
assertEquals(total, target.size());
assertEquals(sum, targetSum);
Map<Object, Integer> target2 = source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator, (t, u) -> t + u));
long target2Sum = 0;
for (Integer value : target2.values()) {
target2Sum += value;
}
log.debug("target2Sum:{}", target2Sum);
System.err.println(String.format("target2Sum:{%d}", target2Sum));
assertEquals(sum, target2Sum);
ConcurrentMap<Object, Integer> target3 = source.parallelStream()
.collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator, (t, u) -> t + u, ConcurrentHashMap::new));
long target3Sum = 0;
for (Integer value : target3.values()) {
target3Sum += value;
}
log.debug("target3Sum:{}", target3Sum);
System.err.println(String.format("target3Sum:{%d}", target3Sum));
assertEquals(sum, target3Sum);
boolean duplicateKeyExceptionForParallelStream = false;
try {
source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator));
} catch (IllegalStateException e) {
duplicateKeyExceptionForParallelStream = true;
}
assertTrue(duplicateKeyExceptionForParallelStream);
boolean duplicateKeyExceptionForStream = false;
try {
source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator));
} catch (IllegalStateException e) {
duplicateKeyExceptionForStream = true;
}
assertTrue(duplicateKeyExceptionForStream);
}
}
分享到:
相关推荐
Parallel and Concurrent Programming in Haskell.pdf Parallel and Concurrent Programming in Haskell.pdf
* parallelStream 的线程池个数默认为 Runtime.getRuntime().availableProcessors() - 1,可以通过设置 -Djava.util.concurrent.ForkJoinPool.common.parallelism 来修改。 * 在阻塞场景下,parallelStream 的优势更...
本书《Parallel and Concurrent Programming in Haskell》由Simon Marlow撰写,面向已经具备Haskell基础知识的读者,旨在指导他们通过Haskell语言的多个API和框架,编写支持并行和并发处理的程序。本书详细介绍了...
《Parallel and Concurrent Programming in Haskell 2013》是Simon Marlow所著的一本关于Haskell编程语言在并行与并发编程方面的专著。在这本书中,作者深入探讨了Haskell语言的并行与并发能力,以及如何有效地利用...
这个工程是为了学习guava concurrent中的AbstractFuture而建立的,里面有可以运行的例子,再配合我的博客:https://blog.csdn.net/o1101574955/article/details/82889851,可以看明白guava concurrent的基本设计思路...
concurrent-1.3.4.jar
backport-util-concurrent.jarbackport-util-concurrent.jarbackport-util-concurrent.jar
标题 "JDK concurrent" 指的是Java开发工具包(JDK)中的并发编程相关知识。并发编程是在多线程环境中同时执行多个任务的技术,它在现代计算机系统中至关重要,尤其是在多核处理器和高并发应用中。Java JDK提供了一...
《backport-util-concurrent:Java并发编程的利器》 在Java的世界里,高效并发处理是提升应用程序性能的关键因素之一。backport-util-concurrent库,正如其名,是一种将Java 5及以上版本的并发特性“回移植”到Java...
concurrent-1.3.2.jar concurrent-1.3.2.jar
Concurrent.Thread.js 一个用来让javascript也进行多线程开发的包,感兴趣的快来下吧。
《并发编程:JavaScript中的Concurrent.Thread.js》 在IT领域,多线程编程是一种常见的优化技术,用于提高程序的执行效率。特别是在JavaScript这样的单线程环境中,由于其异步执行模型,多线程处理显得尤为重要。...
Java的并发库(java.util.concurrent)提供了丰富的线程池实现,包括`ThreadPoolExecutor`,它是基于工作窃取算法的高效线程池。本文主要分析的是基于`concurrent`包的一个特定线程池实现,探讨其实现原理和源码。 ...
标题《Parallel and Concurrent Programming in Haskell》和描述表明,该文件是一本专注于Haskell语言在并行和并发编程领域的深入探讨。Haskell,作为一门纯粹的、懒惰的函数式编程语言,为开发者提供了强大的抽象...
concurrent.jar web开发工具包
在Java编程语言中,"concurrent"一词通常与多线程并发编程相关,它涉及到如何在多个执行线程之间高效、安全地共享资源。这里的"concurrent源代码"可能是指Java并发包(`java.util.concurrent`)中的源代码,这个包包含...