Java callback function details

Source: Internet
Author: User

To understand what callback functions are, read the following information on the Internet:

Document 1:

What is a callback function first?

In Windows, programmers want the system DLL to call a method they have compiled, so they use the callback function (callback) interface in the DLL to compile the program and call it. This is called callback. When calling an interface, you must strictly follow the defined parameters and methods and process the asynchronous function. Otherwise, the program will crash.

This explanation seems difficult. Here is an example of a simple ticket:

Programmer A writes a program (program A), which reserves the callback function interface and encapsulates the program. Programmer B wants a to call a method in program B, So he calls back the method in B through the interface in. .

In C/C ++, a callback function is used. If a function is dropped, the caller must be notified of its own pointer address, but there is no pointer in Java. What should I do? We can use interfaces to define callback functions.

Suppose I am a programmer, and the following is my program:

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 can write the program implementation interface according to my definition.

public interface MyCallInterface {public void method();} 

As a result, programmer B only needs to implement this interface to achieve the purpose of callback:

 

Public Class B implements mycallinterface {public void method () {system. Out. println ("who will call me. So lonely !! ");} Public static void main (string ARGs []) {caller call = new caller (); call. setcallfuc (New B (); call. call ();}}

Document 2:

The so-called callback means that client program c calls a method A in service program s, and then s calls a method B in C at a certain time. For C, this B is called a callback function.

Generally, C does not call B by itself. C provides B for S to call it, and C has to provide it. S does not know who B is provided by C, so s will agree with B's interface specifications (function prototype ), then C tells s to use the B function through a function R of S in advance. This process is called the registration of the callback function, and R is called the registration function.

The following is a common example:

One day, I called you to ask questions. Of course it was a difficult problem. :) you couldn't find a solution at the moment, and I couldn't wait there with a phone, so we agreed: after you have come up with a solution, you can call me to notify me. In this way, I will stop calling to handle other things. After xx minutes, my cell phone rang, and you said with high enthusiasm that the problem had been fixed, so you should handle it like this. The story ends here.

This example illustrates the "Asynchronous + callback" programming mode. When you call a mobile phone to tell me that the result is a "Callback" process. My mobile phone number must be told before. This is the registration callback function; my mobile phone number should be valid and the mobile phone can receive your call. This is the callback function that must comply with the interface specifications.

If you are not quite clear about the description and code:

Declare an interface. The other class has a parameter in the method to be of this interface type, and then implement this interface in another class (in Java, anonymous internal classes are used ), upload the object generated by this anonymous class as the parameter to the class mentioned above, and then implement the callback ....... for this usage, you can refer to the several interfaces commonly used for database operations in Java .....

// Declare an interface public interface icallback {void postexec ();} // another class has a parameter in the method to be the public class foobar {private icallback callback of this interface type; public void setcallback (icallback callback) {This. callback = callback;} public void dosomething () {callback. postexec () ;}---------------------------------------- public class test {public static void main (string [] ARGs) {foobar Foo = new foobar (); Foo. setcallback (New icallback () {public void postexec () {system. out. println ("method executed. ") ;}}); Foo. dosomething (); // call a function }}

Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------

Document 3: a typical example of using callback Functions

The following uses the Java callback function to implement a tool class for testing the function runtime:

If we want to test the execution time of a class method, we usually do this:

Java code

Public class testobject {/*** a method used for testing, and a time-consuming cycle */public static void testmethod () {for (INT I = 0; I <100000000; I ++) {}}/*** a simple test method */Public void testtime () {long begin = system. currenttimemillis (); // Test start time testmethod (); // test method long end = system. currenttimemillis (); // test end time system. out. println ("[use time]:" + (end-begin); // print the time used} public static void main (string [] ARGs) {testobject test = new testobject (); test. testtime ();}}

As you can see, only the "// test method" of the testtime () method needs to be changed. Let's create a function to implement the same function but be more flexible:

First, define a callback interface:

Java code

Public interface callback {// Method for executing the callback operation void execute ();} and then write a tool class: Java code public class tools {/*** test function usage time, use the execute Method * @ Param callback */Public void testtime (callback) {long begin = system. currenttimemillis (); // Test start time callback.exe cute (); // perform the callback operation long end = system. currenttimemillis (); // test end time system. out. println ("[use time]:" + (end-begin); // print the time used} public static void main (string [] ARGs) {tools tool = new tools (); tool. testtime (New callback () {// defines the execute method public void execute () {// here you can add one or more methods testobject to test the running time. testmethod ();}});}}

As you can see, the callback function can be implemented by passing testtime () to the execute () method that defines the callback interface.

 

 

Reprinted please indicate the source of http://blog.csdn.net/shimiso welcome people of insight to join our technical exchange group: 173711587

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.