Understanding of callback mechanisms in Java (small example)

Source: Internet
Author: User

This explanation seems to be more difficult to understand, here is a simple example, 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).

Under normal circumstances, developers use an already defined API, which is called call. But sometimes this does not meet the requirements, it is necessary for the programmer to register their own programs, and then let the prior definition of a lot of APIs at the appropriate time to invoke the method of registration, this is called callback.

"Generally speaking, the callback function is usually written in accordance with the specification of others (John Doe) , waiting for someone else (Zhang San) to call the function, in C, the callback function is usually passed by the function pointer; in Java, Usually the person writing another class or class library (John Doe) specifies an interface, then you (Zhang San) to implement this interface, and then the implementation of an object of the class as a parameter to other people's program, the other person's program will be necessary to use the interface to invoke the function you write. "

usage Tip: Set an interface that declares the method we want to invoke in the interface.

Registering a newly defined callback interface in another method

First step: Define the Callback interface
package com.callback;/** * @since 2012-02-02 * @desc 定义回调接口 */publicinterfaceCallBackInterface {    publicvoiddoSome();          publicvoidexectueMethod();}
Step two: Programs written by John Doe
packagecom.callback;/** * @since 2012-02-02 * @desc 李四 */publicclassMethodB {    publicdoublegetTime(CallBackInterface callBack) {        longstart = System.currentTimeMillis();        callBack.exectueMethod();        longend = System.currentTimeMillis();        System.out.println("cost time="+ (end - start));        returnend - start;    }}

Step three: Zhang San implementation of John Doe-defined interfaces

packagecom.callback;/** * @since 2012-02-02 * @desc 张三 */publicclassMethodA {    publicstatic voidmain(String args[]){        MethodB b=newMethodB();        //返回值d只和MethodB有关,和接口中方法的返回值无关        doubled=b.getTime(newCallBackInterface() {                        //张三实现了李四定义的接口            publicvoidexectueMethod() {                newMethodA().testMethod();            }                        @Override            publicvoiddoSome() {                             }        });        System.out.println("d="+d);    }        publicvoid testMethod(){        for(inti=0;i<10000;i++){            System.out.print("");        }    }}

Output Result:

Cost time=31
d=31.0

Understanding "Callback Functions"

The so-called callback, the client program client calls a function SA in the Service program server, and then the server in turn calls a function in the client CB, for the client, this CB is called the callback function. For example, a window procedure function under Win32 is a typical callback function.
In general, the client does not call Cb,client itself to provide the CB is to let the server to invoke it, and the client has to provide. Because the server does not know who the client provides CB name, so the server will contract the CB Interface Specification (function prototype), and then by the client in advance through the server of a function R tells the server itself will use the CB function, this process is called callback function registration , R is called the registration function. Both Web Serverervicliente and Java RMI Use callback mechanisms to access remote server programs.

Understanding of callback mechanisms in Java (small example)

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.