Class ParallelStreamUtil
java.lang.Object
cloud.opencode.base.core.stream.ParallelStreamUtil
Parallel Stream Utility - Smart parallel/sequential stream selection
并行流工具类 - 智能选择并行/顺序流
Provides utilities for intelligent parallel stream usage based on collection size.
提供基于集合大小智能选择并行流使用的工具。
Features | 主要功能:
- Smart parallel/sequential selection by threshold - 按阈值智能选择并行/顺序流
- CPU-aware threshold calculation - CPU 感知的阈值计算
- Force parallel/sequential methods - 强制并行/顺序流方法
- Parallelism recommendations - 并行推荐
Usage Examples | 使用示例:
// Smart stream selection - 智能流选择
Stream<String> stream = ParallelStreamUtil.stream(list);
// Check if parallel recommended - 检查是否推荐并行
boolean parallel = ParallelStreamUtil.isParallelRecommended(list.size());
// Force parallel - 强制并行
Stream<String> parallel = ParallelStreamUtil.parallelStream(list);
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
- Null-safe: Yes (returns empty stream on null) - 空值安全: 是
Performance | 性能特性:
- Time complexity: O(n/p) where n = elements, p = parallelism - O(n/p), n为元素数, p为并行度
- Space complexity: O(n) for stream pipeline - 流管道 O(n)
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic intGets the available processor count 获取可用处理器数量static intGets the ForkJoinPool parallelism 获取 ForkJoinPool 并行度static intGets the recommended parallel threshold 获取推荐的并行阈值static booleanisParallelRecommended(int size) Checks if parallel stream is recommended 检查是否推荐使用并行流static booleanisParallelRecommended(int size, int threshold) Checks if parallel stream is recommended (custom threshold) 检查是否推荐使用并行流(自定义阈值)static <T> Stream<T> parallelStream(Collection<T> collection) Forces a parallel stream 强制使用并行流static <T> Stream<T> sequentialStream(Collection<T> collection) Forces a sequential stream 强制使用顺序流static <T> Stream<T> stream(Collection<T> collection) Selects sequential or parallel stream based on collection size 根据集合大小智能选择顺序/并行流static <T> Stream<T> stream(Collection<T> collection, int threshold) Selects sequential or parallel stream based on collection size (custom threshold) 根据集合大小智能选择顺序/并行流(自定义阈值)static <T> Stream<T> toParallelIf(Stream<T> stream, boolean condition) Converts the stream to parallel if the condition is met 将流转为并行流(如果条件满足)static <T> Stream<T> toSequential(Stream<T> stream) Converts the stream to sequential 将流转为顺序流
-
Method Details
-
getRecommendedThreshold
public static int getRecommendedThreshold()Gets the recommended parallel threshold 获取推荐的并行阈值基于 CPU 核心数计算,通常为每核心 1000 个元素
-
stream
Selects sequential or parallel stream based on collection size 根据集合大小智能选择顺序/并行流 -
stream
Selects sequential or parallel stream based on collection size (custom threshold) 根据集合大小智能选择顺序/并行流(自定义阈值) -
isParallelRecommended
public static boolean isParallelRecommended(int size) Checks if parallel stream is recommended 检查是否推荐使用并行流 -
isParallelRecommended
public static boolean isParallelRecommended(int size, int threshold) Checks if parallel stream is recommended (custom threshold) 检查是否推荐使用并行流(自定义阈值) -
getAvailableProcessors
public static int getAvailableProcessors()Gets the available processor count 获取可用处理器数量 -
getParallelism
public static int getParallelism()Gets the ForkJoinPool parallelism 获取 ForkJoinPool 并行度 -
sequentialStream
Forces a sequential stream 强制使用顺序流 -
parallelStream
Forces a parallel stream 强制使用并行流 -
toParallelIf
-
toSequential
-