当前位置:网站首页>RPC调用方如何实现异步调用:CompletableFuture
RPC调用方如何实现异步调用:CompletableFuture
2022-07-24 08:33:00 【trigger333】
目录
情景:
每次需要在此阻塞等待结果返回 可能还会触发超时重试 使得后续业务执行受到影响。下面的代码执行是串行执行,并发度低。
long startTime = System.currentTimeMillis();
int threadNum = 10;
for (int i = 0; i < threadNum; i++) {
try {
// 每次需要在此阻塞等待结果返回 可能还会触发超时重试 使得后续业务执行受到影响
String result = dataService.sendData("test" + i);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.println(" time cost : " + (double)((endTime - startTime)/1000) + " s");可以使用 CompletableFuture 进行异步调用和任务编排,改写完变为异步调用,耗时短,任务调用完后会自动执行后续的业务。
int threadNum = 10;
long startTime = System.currentTimeMillis();
for (int i = 0; i < threadNum; i++) {
try {
int finalI = i;
CompletableFuture<String> cf = CompletableFuture.supplyAsync(()->{
String result = dataService.sendData("test" + finalI);
return result;
}).thenCompose(result -> CompletableFuture.supplyAsync(()->{
// 拿到rpc调用的结果后进行相关的操作
System.out.println(result);
return "";
}));
} catch (Exception e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.println(" time cost : " + (double)((endTime - startTime)/1000) + " s");CompletableFuture
上文中的任务编排本质上是把任务交给了CompletableFuture 中的线程池处理,线程池的核心线程数默认是核心数-1。所以大于这个值也会进行 排队等待。解决办法就是自定义线程池,不仅可以起到隔离的效果,也可以根据任务的数量动态的创建特定大小的线程池。
边栏推荐
- Chinese brands in the historical process
- Mysql database advanced
- 国产“火箭心”人工心脏上市 不同人工心脏有什么区别?
- Introduction to wechat authorized login third-party app applet method
- Is gamefi in decline or in the future?
- Detailed explanation of wechat applet page configuration and sitemap configuration parameters
- Do you know the private domain traffic in app?
- Figure storage geabase
- What is the difference between domestic "rocket heart" artificial heart and different artificial heart?
- Draw a circular radar chart with canvas
猜你喜欢

2022.7.11全盘题解

Cososcreator upgrade gradle version
![[redis] how much do you know about bloom filter and cuckoo filter?](/img/f0/8bcbe82bd6d412e68d85450f5894e3.png)
[redis] how much do you know about bloom filter and cuckoo filter?

Hack the box - Introduction to networking module detailed Chinese tutorial

Aquanee: the true meaning of "p2e"

Will Plato become the risk target of the meta universe? Platofarm has great opportunities
![[wechat applet development (III)] realize the stacking and sliding of cards](/img/6c/4ebd60a2106b56b8bf3a6bf17d11f9.png)
[wechat applet development (III)] realize the stacking and sliding of cards

Read and understand move2earn project - move

Web3 traffic aggregation platform starfish OS interprets the "p2e" ecosystem of real business

The code is tired. Stop and enjoy the top color matching~
随机推荐
[Google play access] payment server token acquisition
Summary of points management system project
Golang implements sanggi diagram and analyzes user behavior trajectory
【游戏合集】手机都要被塞爆了,6款优质Pygame游戏合集降临~(附源码)
Okaleido tiger NFT is about to log in to binance NFT platform, and the era of NFT rights and interests is about to start
3587. 连通图(吉林大学考研机试题)
Wargames NATAS (11-15) problem solving essay
Advantages and disadvantages of redis and ZK implementing distributed locks
MySQL counts the total sales data of each month
About the big hole of wechat applet promise
Wei Xiaoli's "pursuer" is coming
Figure storage geabase
The code is tired. Stop and enjoy the top color matching~
Confusing defer, return, named return value, anonymous return value in golang
Implementation of unity hub free version
"Solution" friend of Vulcan
"Move to earn" motion metauniverse, move starts a new journey
[database] complete SQL statement
Brief notes on the key points of distributed system principle introduction
Go: how to gracefully time out