当前位置:网站首页>Using completable future to implement asynchronous callback
Using completable future to implement asynchronous callback
2022-07-22 01:26:00 【Rear cut】
Interface modification based on the example of the boss :https://blog.csdn.net/it_freshman/article/details/80580369
There are several steps to boil a pot of boiling water :
- Add water
- Turn on the gas
- Boil water ing—( More time-consuming actions )
- Observe , Boiling water is finished
- Finally, after the water boils , Turn off the gas
- In the process of boiling water , Don't want to wait , Just opened a game
First, define a water boiling interface :
public interface BoilWater {
void a_addWater() ;
void b_on() ;
void c_boiling() throws Exception ;
void d_off() ;
// Water boiling method
void make() throws Exception;
void playGame() throws Exception ;
}
Next, we implement the interface , This time, we use abstract classes to implement this interface , such make Methods can be implemented without specific :
public abstract class AbstractBoilWater implements BoilWater {
protected volatile boolean isReadyFlag = false;
@Override
public void a_addWater() {
System.out.println("1. Add water ");
}
@Override
public void b_on() {
System.out.println("2. Turn on the gas ");
}
@Override
public void c_boiling() throws Exception {
System.out.println("3-1. In boiling water .....");
Thread.sleep(5000);
System.out.println("3-2. The water is boiling ");
isReadyFlag = true;
}
@Override
public void d_off() {
System.out.println("4. Turn off the gas ");
}
// Water boiling method
@Override
public abstract void make() throws Exception;
@Override
public void playGame() throws Exception {
Thread.sleep(1200);
if (!isReadyFlag) {
System.out.println(" The water is not boiling yet , Play a game ");
}
}
}
Next, call the business , When the water is not good, take the opportunity to play a game , Use CompletableFuture Inherit AbstractBoilWater To make asynchronous calls and listen
public class CompletableFutureBiolWater extends AbstractBoilWater {
@Override
public void make() throws Exception {
a_addWater();
b_on();
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
try {
c_boiling();
} catch (Exception e) {
e.printStackTrace();
}
return 1;
});
// Registration events “ monitor ”
future.whenComplete((v, e) -> {
System.out.println(v);
System.out.println(e);
d_off();
});
while (!future.isDone()) {
playGame();
}
}
}
Write a test class to test
public class testMain {
public static void main(String[] args) {
CompletableFutureBiolWater completableFutureBiolWater= new CompletableFutureBiolWater();
try {
completableFutureBiolWater.make();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The effect is as follows :
remarks , If it's not just the process of boiling water , Maybe boiling water and cutting firewood can be done at the same time , Then it can be transformed into
public class CompletableFutureBiolWater extends AbstractBoilWater {
@Override
public void make() throws Exception {
a_addWater();
b_on();
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
try {
c_boiling();
} catch (Exception e) {
e.printStackTrace();
}
return 1;
});
// Registration events “ monitor ”
future.whenComplete((v, e) -> {
System.out.println(v);
System.out.println(e);
d_off();
});
// Simulate cutting firewood
CompletableFuture<Integer> futurekanchai = CompletableFuture.supplyAsync(() -> {
try {
c_boiling();
} catch (Exception e) {
e.printStackTrace();
}
return 2;
});
// Registration events “ monitor ”
futurekanchai.whenComplete((v, e) -> {
System.out.println(v);
System.out.println(e);
d_off();
});
while (!future.isDone()&&!futurekanchai.isDone()) {
playGame();
}
}
}
It leads to thinking : In the work order process , Maybe our order actually goes through several processes , At this time, we can use the idea of asynchronous process to design , Reduce synchronization blocking time ; The purpose of defining interfaces here is to implement interfaces for methods , Of course, you can also use engineering mode to optimize
边栏推荐
- 铜牛机房项目的优势和劣势
- 如何测试webservice接口
- Mysql05(视图)
- MongoDB 中的管道操作符($group、$unwind、$sort、$limit、$skip)
- A 股指数历史数据 API 数据接口
- Introduction to common interfaces of vector
- [terminal _1]-xshell 5 the hottest terminal software!
- Selenium怎么上传文件,比你想的方式还多
- nslookup命令使用
- 45: Chapter 4: developing file services: 6: third party cloud storage solutions [Alibaba cloud OSS]; (purchase OSS services; subscribe to services; create a bucket;)
猜你喜欢
Niu Ke brushes question 01 - Kiki de duplication of integers and sorting (C language)
45: Chapter 4: developing file services: 6: third party cloud storage solutions [Alibaba cloud OSS]; (purchase OSS services; subscribe to services; create a bucket;)
vector的常见接口介绍
Learn the necessary tools of automation selenium think about automated testing in the pit again
近红外染料CY7.5标记PNA多肽实验步骤CY7.5-PNA|188Re标记反基因肽核酸(AGPNA)
Uniapp introduces Tencent map
【微信小程序】解决代码上传超过大小限制,小程序分包
多智能体系统集群协同控制实验平台详解与典型案例
Basic usage of Networkx
[completion course] development process of IOT / embedded / MCU graduation design project
随机推荐
【sciter】:窗口通信
怎么学自动化测试,可以自学吗?
[data analysis 01]
开源demo| ARCall 小程序开源示例发布
Vscode running C language file
docker搭建redis及集群
A 股分笔交易数据 API 数据接口
46: Chapter 4: develop file services: 7: integrate Alibaba OSS in [files] file services;
【愚公系列】2022年7月 Go教学课程 014-运算符之算术运算符
音频自动增益控制 AGC 解决的问题及原理解析
The sandbox teamed up with agoria to build agoriaverse
lua环境配置
[rm_ee_note] 2 serial port & remote control
A 股指数历史数据 API 数据接口
Mysql07(数据更新DML)
【学习笔记】AGC008
Visualization: you must know these ten data visualization tool software platforms
Pycharm使用教程:5个非常有用的技巧
多智能体系统集群协同控制实验平台详解与典型案例
What is the difference between int *const p= & I and int const *p= & I