java并行之parallelStream与CompletableFuture比较
1.
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList; public class CompletableFutureTest {
static final UserFeatureable[] UserFeatures = {new GetCareerUserFeature(), new GetTradeUserFeature(), new
GetVipLevelUserFeature()}; public static void main(String[] args) {
int id = 10001;
List<UserFeatureable> userFeatures = Arrays.asList(UserFeatures);
long startTime = System.currentTimeMillis();
String result = userFeatures.parallelStream().map(p -> p.getKey() + ":" + p.getValue(id)).collect(joining(","));
long endTime = System.currentTimeMillis();
System.out.println(String.format("parallelStream消耗时间:%d,返回结果:%s", (endTime - startTime), result)); startTime = System.currentTimeMillis();
List<Future<String>> futureList = userFeatures.stream().map(
p -> CompletableFuture.supplyAsync(() -> p.getKey() + ":" + p.getValue(id)))
.collect(toList());
result = futureList.stream().map(p->getVal(p,"")).collect(joining(","));
endTime = System.currentTimeMillis();
System.out.println(String.format("CompletableFuture的默认的ForkJoin线程池消耗时间:%d,返回结果:%s", (endTime - startTime),
result)); //当userFeature越多,使用自定义线程池更有利
startTime = System.currentTimeMillis();
futureList = userFeatures.stream().map(
p -> CompletableFuture.supplyAsync(() -> p.getKey() + ":" + p.getValue(id),CustomThreadPool.INSTANCE))
.collect(toList());
result = futureList.stream().map(p->getVal(p,"")).collect(joining(","));
endTime = System.currentTimeMillis();
System.out.println(String.format("CompletableFuture的自定义线程池消耗时间:%d,返回结果:%s", (endTime - startTime), result));
} private static <T>T getVal(Future<T> future,T defaultV){
try {
return future.get(2,TimeUnit.SECONDS);
}catch (Exception ex){
return defaultV;
}
}
} interface UserFeatureable {
String getKey(); String getValue(int id);
} class GetCareerUserFeature implements UserFeatureable { @Override
public String getKey() {
return "career";
} @Override
public String getValue(int id) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) { }
return "10";
}
} class GetTradeUserFeature implements UserFeatureable { @Override
public String getKey() {
return "trade";
} @Override
public String getValue(int id) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) { }
return "5";
}
} class GetVipLevelUserFeature implements UserFeatureable { @Override
public String getKey() {
return "vip";
} @Override
public String getValue(int id) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) { }
return "v1";
}
}
2.自定义线程池配置
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; public class CustomThreadPool {
/**
* 默认核心线程池大小
*/
private static final int DEFAULT_CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors(); /**
* 最大线程池大小
* 最佳线程数目 = (线程等待时间与线程CPU时间之比 + 1)* CPU数目
* 最佳线程数目 = (1s/0.1s + 1) * CPU数目
*/
private static final int DEFAULT_MAXIMUM_POOL_SIZE = 11 * DEFAULT_CORE_POOL_SIZE; /**
* 超过核心线程后,空闲线程等待时间
*/
private static final long DEFAULT_KEEP_ALIVE_SECONDS = 10; /**
* 等待执行的线程队列
*/
private static BlockingQueue<Runnable> WORK_QUEUE = new LinkedBlockingDeque(DEFAULT_MAXIMUM_POOL_SIZE * 2); public static ThreadPoolExecutor INSTANCE = new ThreadPoolExecutor(
DEFAULT_CORE_POOL_SIZE,
DEFAULT_MAXIMUM_POOL_SIZE,
DEFAULT_KEEP_ALIVE_SECONDS,
TimeUnit.SECONDS,
WORK_QUEUE,
new ThreadFactoryBuilder().setNameFormat("task-pool-thread-%d").build()); }
3.结果
parallelStream消耗时间:2889,返回结果:career:10,trade:5,vip:v1
CompletableFuture的ForkJoin线程池消耗时间:1010,返回结果:career:10,trade:5,vip:v1
CompletableFuture的自定义线程池消耗时间:1011,返回结果:career:10,trade:5,vip:v1
java并行之parallelStream与CompletableFuture比较的更多相关文章
- Java 并行与并发
		Java 并行与并发 注意两个词:并行(Concurrent) 并发(Parallel) 并行:是逻辑上同时发生,指在某一个时间内同时运行多个程序 并发:是物理上同时发生,指在某一个时间点同时运行多个 ... 
- Java并发程序设计(二)Java并行程序基础
		Java并行程序基础 一.线程的生命周期 其中blocked和waiting的区别: 作者:赵老师链接:https://www.zhihu.com/question/27654579/answer/1 ... 
- JAVA并行程序基础
		JAVA并行程序基础 一.有关线程你必须知道的事 进程与线程 在等待面向线程设计的计算机结构中,进程是线程的容器.我们都知道,程序是对于指令.数据及其组织形式的描述,而进程是程序的实体. 线程是轻量级 ... 
- 避坑 | Java8使用并行流(ParallelStream)注意事项
		示例分析 /** * 避坑 | Java8使用并行流(ParallelStream)注意事项 * * @author WH.L * @date 2020/12/26 17:14 */ public c ... 
- JAVA并行程序基础二
		JAVA并行程序基础二 线程组 当一个系统中,如果线程较多并且功能分配比较明确,可以将相同功能的线程放入同一个线程组里. activeCount()可获得活动线程的总数,由于线程是动态的只能获取一个估 ... 
- JAVA并行程序基础一
		JAVA并行程序基础一 线程的状态 初始线程:线程的基本操作 1. 新建线程 新建线程只需要使用new关键字创建一个线程对象,并且用start() ,线程start()之后会执行run()方法 不要直 ... 
- Java8使用并行流(ParallelStream)注意事项
		Java8并行流ParallelStream和Stream的区别就是支持并行执行,提高程序运行效率.但是如果使用不当可能会发生线程安全的问题.Demo如下: public static void co ... 
- JAVA使用并行流(ParallelStream)时要注意的一些问题
		https://blog.csdn.net/xuxiaoyinliu/article/details/73040808 
- Java:并行编程及同步使用方法
		知道java可以使用java.util.concurrent包下的 CountDownLatch ExecutorService Future Callable 实现并行编程,并在并行线程同步时,用起 ... 
随机推荐
- linq join一些忘记的操作
- Basic4android v3.00 发布
			这次发布的版本主要是增加了快速debuger. 在运行时,可以在IDE 里面随时修改代码,而不需要重新发布应用. 大大提高了开发效率. Basic4android v3.00 is released. ... 
- String 、 StringBuffer 和 StringBuilder
			StringBuffer (一个线程安全的可变字符串序列,用于多线程) A thread-safe, mutable sequence of characters. StringBuilder (可变 ... 
- Thrift线程和状态机分析
			目录 目录 1 1. 工作线程和IO线程 1 2. TNonblockingServer::TConnection::transition() 2 3. RPC函数被调用过程 3 4. 管道和任务队列 ... 
- Window vista 以上制作自定义证书并为端口配置ssl
			此处的关键在于证书需要分两步,不然在配置ssl时总会有错误.也许makecert命令也会有些玄机,但是管他呢,请按以下步骤和命令配置,几分钟就可成功: 证书制作: 1, 在开始/所有程序(或其他地方 ... 
- Android-FileUtils工具类
			文件相关工具类 public final class FileUtils { private FileUtils() { throw new UnsupportedOperationException ... 
- SQLSERVER带端口号的链接方式
			SQLSERVER带端口号的链接方式 <add key="BBAcnn" value="server=IP,端口号\实例名;database=TESTDB;uid= ... 
- Dom4j的一个小例子,用于解析xml文件的元素获取方式(转)
			import java.io.File; import java.io.IOException; import javax.xml.parsers.ParserConfigurationExcepti ... 
- Tempdb--TempDB Basic
			1. TempDB只能运行在Simple Recovery Model下 2. 由于TempDB不需要Recovery,因此在TempDB中发生的操作不需要REDO,因此在日志记录上有别于其他数据库. ... 
- PYQT4 Python GUI 编写与 打包.exe程序
			工作中需要开发一个小工具,简单的UI界面可以很好的提高工具的实用性,由此开启了我的第一次GUI开发之旅,下面将自己学习的心得记录一下,也做为学习笔记吧!!! 参考:http://www.qaulau. ... 
