Java callback回调
2024-09-06 11:46:04
package com.callback; public interface CSCallBack {
public void process(String status);
}
package com.callback; public class Client implements CSCallBack { private Server server; public Client(Server server) {
this.server = server;
} public void sendMsg(final String msg){
System.out.println("客户端:发送的消息为:" + msg);
new Thread(new Runnable() {
@Override
public void run() {
server.getClientMsg(Client.this,msg);
}
}).start();
System.out.println("客户端:异步发送成功");
} @Override
public void process(String status) {
System.out.println("客户端:服务端回调状态为:" + status);
}
}
package com.callback; public class Server { public void getClientMsg(CSCallBack csCallBack , String msg) {
System.out.println("服务端:服务端接收到客户端发送的消息为:" + msg); // 模拟服务端需要对数据处理
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("服务端:数据处理成功,返回成功状态 200");
String status = "200";
csCallBack.process(status);
}
}
package com.callback; public class CallBackTest {
public static void main(String[] args) {
Server server = new Server();
Client client = new Client(server); client.sendMsg("Server,Hello~");
}
}
最新文章
- google protobuf安装与使用
- Selenium生成Report的利器- ExtentReports
- WPF 样式和行为
- mysql TRUNCATE
- ASP.NET中如何删除最近打开的项目和文件的记录
- oracle LogMiner配置使用
- How to install MP4box on CentOS 6
- 转载:JSONObject简介
- 创建XML文件
- 宣布发布 Windows Azure SDK 2.2,正式发布 Windows Azure Backup 和 Hyper-V 恢复管理器预览版
- Andrew Ng机器学习课程笔记--week3(逻辑回归&;正则化参数)
- AtCoder Grand Contest 030 (AGC030) C - Coloring Torus 构造
- 图像特征的提取(gaussian,gabor,frangi,hessian,Morphology...)及将图片保存为txt文件
- ubuntu中的环境变量
- weblogic清除缓存
- java中Future与FutureTask使用与分析
- HTML中head与body标签
- java后台代码发送邮件
- 【Python学习笔记】Jupyter Lab目录插件安装
- 【线段树】【扫描线】Petrozavodsk Winter Training Camp 2018 Day 5: Grand Prix of Korea, Sunday, February 4, 2018 Problem A. Donut
热门文章
- Python协程之Gevent模块
- Shell基本语法---函数
- 题解 洛谷 P4098 【[HEOI2013]ALO 】
- 题解 洛谷 P6378 【[PA2010]Riddle】
- windows如何解决Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion'
- 使用queue 做一个分布式爬虫(一)
- web自动化 -- HTMLreport(三)测试报告输出log日志
- Java基础之java8新特性(1)Lambda
- NoSQL和SQL怎么选用?
- Python修改元组