Understanding of callback functions in Java

Source: Internet
Author: User

Don't leave a little detail in your study, maybe he'll decide your success or failure.

This is the definition in C/s + +:

A callback function is a function that is called through a function pointer (function address). If you pass a pointer to a function (that is, the address of a function) as an argument to another function, it is called a callback of the function when the function it points to is called through this pointer.
A callback function is not called directly by the implementing party of the function, but is called by another party when a particular event or condition occurs, and is used to respond to the event or condition.

There is always a certain interface between software modules, which can be divided into three categories from the way they are called:
Synchronous calls, callbacks, and asynchronous calls.
A synchronous call is a blocking call, and the caller waits for the other party to complete before returning, which is a one-way invocation;
A callback is a two-way invocation pattern, that is, the callee calls the interface when the interface is called;
An asynchronous invocation is a mechanism similar to a message or event, but it is called in the opposite direction, and the service of the interface proactively notifies the client (that is, the interface that invokes the client) when a message is received or an event occurs.


The callback function mechanism provides the system's ability to handle asynchronous events. First, the asynchronous event occurs when the code that needs to be executed into a function, and the function is registered as a callback function, so that when the asynchronous event occurs, the system will automatically invoke the pre-registered callback function, the registration of the callback function is actually to fill the callback function information into a struct variable to register the callback function.

< Span style= "font-family: Italics _gb2312" >  the so-called callback is the client program C called a function A in the service program S, Then s again at some point to call a function B in C, for C, this b is called a callback function. For example, a window procedure function under Win32 is a typical callback function. Generally speaking, C does not call B,c itself to provide B's purpose is to let s to invoke it, and C has to provide. Since s does not know B name provided by C, S will contract B's interface specification (function prototype), and then by C advance through S's a function R tells S itself to use the B function, this process is called the callback function registration, R is called the registration function. The RMI for WEB service and java uses a callback mechanism to access the remote server program.


Here's a popular example:
One day, I call to ask you questions, of course, is a problem, ^_^, you can not think of a solution, I can't hold the phone in there silly, so we agreed: Wait until you come up with a way to call me, so I hung off the phone to do other things. After xx minutes, my cell phone rang, you cheerfully said that the problem has been taken care of, should be treated as such. The story ends here. This example illustrates the programming pattern of asynchronous + callback. Which, you later on the phone to tell me the result is a "callback" process; my mobile phone number must be told you before, this is the registration callback function, my mobile phone number should be valid and the phone can receive your call, this is the callback function must conform to the interface specification.


Through the above personal feel to the callback more application is to combine asynchronous. For example: In Ajax, JS communicates asynchronously through components and servers.

Cases:

programmer A wrote a program (program a), which reserved a callback function interface, and encapsulated the program. Programmer B wants A to invoke a method in its own program B, so he callbacks his method in B through the interface in a. Objective to achieve. in C + +, to use a callback function, the dropped function needs to tell the caller their own pointer address, but there is no pointer in Java, what to do? We can implement the definition callback function through the interface (interface).
Let's say I'm programmer a, and here's my program A:


public class Caller  {public      mycallinterface MC;        public void Setcallfuc (Mycallinterface mc)      {         this.mc= mc;      }        public void Call () {         this.mc.method ();      }  }      

I also need to define an interface so that programmer B implements the interface based on my definition of programming.


Public interface Mycallinterface  {public      void method ();      

So, programmer B only needs to implement this interface to achieve the purpose of the callback:

public class B implements Mycallinterface  {public      void method ()      {         System.out.println ("callback");      }        public static void Main (String args[])      {         Caller call = new Caller ();         CALL.SETCALLFUC (New B ());         Call.call ();      }  }  



Understanding of callback functions in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.