Summarize the knowledge of Java callback mechanism

  • 2021-10-13 07:48:13
  • OfStack

Step 1 Callback

Callbacks are divided into synchronous callbacks and asynchronous callbacks, If the scene of buying lottery tickets is used to simulate, I buy lottery tickets, Call the lottery network, Give me the results returned to determine whether to win the prize, A synchronous callback is, After I bought the lottery ticket, I need to wait for the result returned by the lottery network, I can't do anything else at this time, I have to wait for this result, This is called a synchronous callback, Synchronization means waiting, I can't do anything else, You have to wait, An asynchronous callback is, After I bought the lottery ticket, You can do other things, Then when the lottery network has the results and messages, Give me a message back, The most obvious way is in the function of getting lottery results, Add 1 other method, if my other method can be executed immediately, then it is asynchronous (it takes a long time to give whether to win or not), and in the test function, the front and back two, which occur in the thread of the test function, must be in order before and after 1, which is not the place to show synchronization and asynchronism.

That is, I call your function and you call my function. Generally speaking, the function a () of class A calls the function b () of class B, and when the function b () of class B is executed, it calls the function a1 () of class A. Is a two-way call. 1 In general, there are two types of callbacks, synchronous callbacks and asynchronous callbacks.

2. Synchronize callbacks

A two-way call mode in which the callee calls the other's function when the function is called, without doing other operations in the middle.

For example, where CalImp belongs to class A and CalLogin belongs to class B.


public interface ICallBack {
    public void calResult(int a,int b,int result);
}
 
public class CalImpl implements ICallBack{
 
    private int a;
    private int b;
 
    public CalImpl(int a, int b){
        this.a = a;
        this.b = b;
    }
 
    public void calculate(){
       new CalLogic().calculateLogic(a,b,this);
    }
 
    @Override
    public void calResult(int a, int b, int result) {
        // Console output 
        System.out.println(a + " + " + b + " = " + result);
    }
}
 
public class CalLogic {
 
    public void calculateLogic(int a,int b,ICallBack iCallBack){
        int result = a + b;
        System.out.println(" The called thread completes execution ...");
        iCallBack.calResult(a, b, result);
    }
 
}
 
public class TestCallBack {
 
    public static void main(String[] args) {
        int a = 22;
        int b = 33;
        // Instantiate calculator Calculator Class 
        CalImpl calculator = new CalImpl(a,b);
        // Invoke Calculator calculator Calculation function of 
        calculator.calculate();
        System.out.println(" Main thread execution completed ...");
 
        //22 + 33 = 55
        // Main thread execution completed ...
    }
}

3. Asynchronous callback

A mechanism similar to a message or event. When the callee function receives a message or an event (completes an operation), it calls the other function, that is, it is notified by asynchronous message.

To put it simply, The a () function of class A calls the b () function of class B, However, b () function is time-consuming, and it is uncertain when it is executed. If it is a synchronous call, it will wait for b () to execute the function in the callback class A. If it is an asynchronous callback, it calls b () function. Although b () function is not executed, a class still continues to execute. In order to complete this, it is necessary to open another thread.

For example, where CalImp belongs to class A and CalLogin belongs to class B. Class A has a new thread to execute.

The output thread is finished... it was put at the end of the code, but the output was executed first, because another thread was opened.

4. Difference between synchronous and asynchronous callbacks

The biggest difference between asynchronous callback and synchronous callback is that a new subthread is created in asynchronous callback.

5. Asynchronous Scenarios

Asynchronous callbacks are common when requesting server data, and callbacks are made when data is fetched.


Related articles: