什么是java中的回调 [英] What is a callback in java

查看:517
本文介绍了什么是java中的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

什么是回调函数?

我看过维基百科定义回调,但我仍然没有得到它。任何人都可以解释一下回调是什么,尤其是以下行

I have read the wikipedia definition of a callback but I still didn't get it. Can anyone explain me what a callback is, especially the following line


在计算机编程中,回调是对可执行代码的引用,或者一段可执行代码,作为参数传递给其他代码。这允许较低级别的软件层调用更高级别层中定义的子例程(或函数)。

In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.


推荐答案

也许一个例子会有所帮助。

Maybe an example would help.

您的应用程序想从某个远程计算机下载文件,然后写入本地磁盘。远程计算机是拨号调制解调器和卫星链路的另一端。延迟和传输时间将是巨大的,您还有其他事情要做。所以,你有一个函数/方法将缓冲区写入磁盘。您将指向此方法的指针与远程URI和其他内容一起传递给您的网络API。此网络呼叫会立即返回,您可以执行其他操作。 30秒后,来自远程计算机的第一个缓冲区到达网络层。然后,网络层调用您在设置期间传递的函数,因此缓冲区将写入磁盘 - 网络层已回调。请注意,在此示例中,回调将发生在网络层线程而不是原始线程上,但这无关紧要 - 缓冲区仍会写入磁盘。

Your app wants to download a file from some remote computer and then write to to a local disk. The remote computer is the other side of a dial-up modem and a satellite link. The latency and transfer time will be huge and you have other things to do. So, you have a function/method that will write a buffer to disk. You pass a pointer to this method to your network API, together with the remote URI and other stuff. This network call returns 'immediately' and you can do your other stuff. 30 seconds later, the first buffer from the remote computer arrives at the network layer. The network layer then calls the function that you passed during the setup and so the buffer gets written to disk - the network layer has 'called back'. Note that, in this example, the callback would happen on a network layer thread than the originating thread, but that does not matter - the buffer still gets written to the disk.

这篇关于什么是java中的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆