Synchronous calls, callbacks, and asynchronous calls (Callback_function)

Source: Internet
Author: User

Original address: https://www.cnblogs.com/WalsonWang/p/4745116.html

There is always a certain interface between modules, which can be divided into three categories: synchronous invocation, callback, and asynchronous invocation. A synchronous call is a blocking call, we often use it in writing programs; callbacks are a two-way invocation pattern, which means that the invoked interface is invoked to invoke the other's interface, which may be a bit of a detour, as illustrated later in the article; asynchronous invocation is a mechanism similar to a message or event. Solve the problem of synchronization blocking, for example: a notice B, they go each way, do not affect each other, do not like synchronous call, a notice B, must wait until B after the end, a to continue walking. Callbacks are fundamental to asynchrony, so the callback mechanism is highlighted below.

For the moment we will not discuss some of the terms and mechanisms of the callback, first of all why there is a callback such a call. The emergence of synchronous and asynchronous mechanisms needless to say, we all know why the callback mechanism appears. In our real life, there is a scenario where a boss is busy, he doesn't have time to stare at his employees, and then he tells his employees to finish what they are doing and tell them the results. This example is actually a callback + asynchronous example, another example, a programmer wrote a program A, which reserved the callback function interface, and encapsulated the program, Programmer b Let a call a method in its own program B, so, he through the interface in a to callback his method in B, You may be indefinitely here, and you will continue to explain why the callback occurred. Next we turn the example above into code and see that many people on the Internet finally confuse the asynchronous and callback, so the example does not include an asynchronous call. (Note: The callback is not a solution to what the call time is too long, it is asynchronous.) )

First create a callback interface so the boss has to tell you how to find him in the end: Leave the boss's office address:

[Java] View plaincopy
Package net.easyway.test;

/**
* This interface is the way to contact, whether it's a phone number or a contact address, as
* The boss has to implement this interface
* @author Administrator
*
*/
Public interface Callbackinterface {

public void execute ();  

}
Create a callback object, the boss himself, because the employee to call him after work, so the boss must implement the callback interface, or where the employee to find the boss.

[Java] View plaincopy
Package net.easyway.test;

/**
* The boss appears as a top application, the lower application (employee) is not aware
* What are the methods so he wants to be called by the downlevel Application (employee) must implement this interface
* @author Administrator
*
*/
public class Boss implements Callbackinterface {

@Override public  
Void execute () {  
    System.out.println () received ... "+ System.currenttimemillis ());  

}  

}
Create a control class, that is, the employee object, he must hold the boss's address (callback interface), even if the boss changed a stubble, the office will not change, always find the corresponding boss.

[Java] View plaincopy
Package net.easyway.test;

/**
* Employee class, it must be remembered that this is a low-level class, the bottom is not understand the upper service
* @author Administrator
*
*/
public class Employee {

Private Callbackinterface callBack = null;  

Tell the boss how to contact, that is, register public  
void Setcallback (Callbackinterface callBack) {  
    this.callback = callBack;  
}  

Workers work public  
void Dosome () {  
    //1. Get to work. For  
    (int i=0;i<10;i++) {  
        System.out.println ("First" "+ i +" " It's done. ");  
    }  

    2. Tell the boss to finish  
    Callback.execute ();  
  

}
Test Class Code:

[Java] View plaincopy
Package net.easyway.test;

public class Client {

public static void Main (string[] args) {  


    employee EMP = new Employee ();  

    The callback object (the upper object) is passed in, registered  
    Emp.setcallback (new Boss ());  

    Open the Controller object to run  
    emp.dosome ();  
}  

}
The above example, you can compare with programmer A and programmer B's example.

Looking at the above example, some people may think that this is not interface-oriented programming. How is the callback, you think again, we interface-oriented programming of the call relationship. In layer three, when the business layer calls the data tier, there is no need to pass the business layer itself to the data tier, and this is a high-level call to the bottom of the relationship, such as we use the framework, the general direct call to the framework provided by the API can be, but the callback is different, when the framework does not meet the requirements, We want the framework to call our own class method and how to do it. There's no way to change the frame. Many of the best frame references are available in almost all of the relevant interfaces, we just need to implement the relevant interface, we can complete the registration, and then let the framework call our own class at the right time, remember that when we use struts, when we write the action, we have to inherit the action class, Then implement the Execute () method, write our own business logic code in the Execute () method, and complete the processing of the user request. It can be speculated that the framework and container will provide a large number of callback interfaces to meet personalized customization.

I do not know the above example to understand not. We can now imagine the difference between the filter and the interceptor, which is the biggest difference is that the filter is based on a callback function, requires the support of the container, no container is unable to callback Dofilter () method, And Interceptor is based on the Java reflection mechanism, and container-independent. Does that make the reflex and the callback confusing again? Please see my blog of Java Dynamic Agent "to this length, complement the short--AOP (agent mode)."

In short, to be clear, the first thing is to understand the reasons for the callback function, that is, the application of the scene in order to find out the callback mechanism, or less.

Finally, to give another example, in order to make our function close to the perfect, part of the function outsourced to others, so that others personalized customization, as for how others do, I have to do is to define the relevant interface, this design allows the underlying code to call high-level defined subroutine, enhance the flexibility of the program, and reflection have the same thing, I think this is the real reason for the callback, the above is my personal understanding, hope to discuss.

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.