Explain the three calling mechanisms of java in detail (synchronous callback and asynchronous)

  • 2021-07-13 05:14:18
  • OfStack

1: Synchronous invocation: A blocking invocation, in which the caller waits for the other party to finish executing before returning. It is a one-way invocation
2: Callback: A bi-directional invocation mode, that is, the callee will also call the interface of the other party when the interface is called;
3: Asynchronous invocation: A mechanism similar to a message or event, but in the opposite direction, the service of the interface will actively notify the client when it receives a message or an event (i.e., call the client's interface)

Specifically, a method C in B class is called in A class, and then the method D in A class is called in B class. This method is called callback method.

Example 1: Use Timer in java to send notifications at given time intervals and print data every 10 seconds

TimePrinter implements the ActionListener interface, in which the actionPerformed method is the callback function


import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
/**
 *TimePrinter Implement the callback function interface 
 */
public class TimePrinter implements ActionListener {

  @Override
  public void actionPerformed(ActionEvent event) {
    // TODO Auto-generated method stub
    Date now=new Date();
    System.out.println("Now time is "+now);
    Toolkit.getDefaultToolkit().beep();
  }

}

Test end


import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class TimerTest {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    ActionListener listener=new TimePrinter();
    Timer timer=new Timer(10000, listener);
    timer.start();
    JOptionPane.showMessageDialog(null, "quit");
    System.exit(0);
  }

}

Running results: Print every 10s, and wait patiently after running the program.

Now time is Fri Apr 15 22:31:53 CST 2016
Now time is Fri Apr 15 22:32:03 CST 2016
Now time is Fri Apr 15 22:32:13 CST 2016

It is found from the results that Timer calls back the method in listener every 10s.

Example 2: Teachers usually can't wait and supervise students to complete tasks after students assign them. Teachers usually tell students to call or send a message after tasks are completed, so the process of students returning results to teachers needs teacher information, which is a callback process.


public interface Callback {

  public void taskResult(String name);
}

/**
 *  The student must instruct the teacher's information in order to report the task situation, so it must implement the callback interface 
 *
 */
public class Teacher implements Callback{

  @Override
  public void taskResult(String name) {
    // TODO Auto-generated method stub
    System.out.println(" Mission :"+name+" Finish ");
  }

}

public class Student {
  Callback callback=null;
  // Give the teacher's contact information to the students 
  public void setCallback(Callback callback)
  {
    this.callback=callback;
  }
   public void doTask()
   {
     for(int m=1;m<6;m++)
     {
       callback.taskResult(m+" It's Zhang 3");
     }
   }
}

Test side:


public class CallbackTest {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Student student=new Student();
    student.setCallback(new Teacher());
    student.doTask();
  }

}

Run results:

Task: 1 is completed by 3
Task: 2 is completed by 3
Task: 3 is completed by 3
Task: 4 is completed by 3
Task: 5 is completed by 3


Related articles: