关于回调函数

https://blog.csdn.net/a_running_wolf/article/details/49359923/

这篇文章

在Android的学习过程中经常会听到或者见到“回调”这个词,那么什么是回调呢?所谓的回调函数就是:在A类中定义了一个方法,这个方法中用到了一个接口和该接口中的抽象方法,但是抽象方法没有具体的实现,需要B类去实现,B类实现该方法后,它本身不会去调用该方法,而是传递给A类,供A类去调用,这种机制就称为回调。
---------------------
作者:衷水木
来源:CSDN
原文:https://blog.csdn.net/a_running_wolf/article/details/49359923?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!

这是作者对回调的理解

总觉得差点意思,大概自己理解不深,所以也说不上来,

下图是我在工作中做的一个类似的,调用方法处是接收了来自客户端的请求,然后像netty通道写消息,又要同步在限定时间内等待client返回,

业务代码需要传入自己的请求,时间限定,以及对按时返回和超时返回的处理

首先这是一个异步调用,请求发起后处于await的状态,需要拿到返回才会countdown继续执行,基于返回是否是成功处理,这是一个回调。

waitingThreadPool.newWaitingThread(centralPlatformReq, WaitingThreadPool.WAIT_5_SECONDS, new HandleOntime() {
@Override
public void handle(String uuid) {
propertyResp.setFailedCount(0 + "");
}
}, new HandleOvertime() {
@Override
public void handle(String uuid) {
int resp = Math.toIntExact(messageReids.getRespSize(uuid));
// 由于目前返回的指令很多不包含短地址等标识设备的信息,所以现阶段只返回失败的指令个数
// 不返回失败的指令清单
int failcount = centralPlatformReq.getDevIds().size() - resp;
propertyResp.setFailedCount(failcount + "");
}
});
public void newWaitingThread(
CentralPlatformReq req,
//String uuid,
long waitingTime,
HandleOntime handleOntime,
HandleOvertime handleOvertime) {
String uuid = IdUtil.getId()+"";
if(req.getDevIds()==null||req.getDevIds().isEmpty())
{
return;
}
CountDownLatch latch = newReq(req, uuid);

// 发送请求完毕,同步等待五秒
try {
if (latch.await(waitingTime, TimeUnit.MILLISECONDS)) {
// 5s内返回了
handleOntime.handle(uuid);
} else {
// 超时
handleOvertime.handle(uuid);
}

} catch (InterruptedException e) {
e.printStackTrace();
}
reqEnd(uuid);
}
原文地址:https://www.cnblogs.com/heroinss/p/9811790.html