parallelStream concurrent secure parallelStreamconcurrentsecure
程序员文章站
2024-03-06 19:28:56
...
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); } }