There is always a certain interface between software modules, which can be divided into three categories: synchronous invocation, callback and asynchronous invocation. A synchronous call is a blocking call that is returned when the caller waits for the execution to complete, a one-way invocation, or a two-way invocation pattern, that is, the callee invokes the interface when the interface is called, and the asynchronous invocation is a mechanism similar to a message or event, but it is called in the opposite direction. The service of an interface notifies the client (that is, the interface that invokes the client) when it receives a message or an event occurs. Callbacks are very closely related to asynchronous calls, and we typically use callbacks to register asynchronous messages and to implement notification of messages through asynchronous calls. Synchronous calls are the simplest of the three, and callbacks are often the basis for asynchronous calls
Callback you can understand this:
A send a message to B,b to handle a required thing, the result is returned to a,a and then the result of B returned to do further processing.
Find a using callbacks with asynchronous calls A very good example, from the http://kt8668.iteye.com/blog/205739
A, implementation of callbacks
/** * Callback Interface * @author Kook * */public interface CallBack {/** * Execute callback method * @param objects returns the processed result as a parameter to the callback method */public void exe Cute (Object ... objects);}
B, sender of the message
/** * simple local send Asynchronous message class * @author KOOK * */public class Local implements callback,runnable{/** * A class that receives messages remotely, simulates point-to-point */private remote remote;/** * sent out the message */private string message;public local (Remote remote, string message) {super (); this.remote = remote;this.message = message;} /** * sends the message */public void sendmessage () {/** The name of the current thread **/system.out.println ( Thread.CurrentThread (). GetName ())/** create a new thread to send the message **/thread thread = new thread (this); Thread.Start ();/** The current thread continues execution **/system.out.println ("message has been sent by local~!");} /** * callback function after sending message */public void execute (object... objects ) {/** print returned message **/system.out.println (objects[0])/** print the thread name **/system.out.println (Thread.CurrentThread () to send the message. GetName ());/** Interrupts the thread that sent the message **/thread.interrupted ();} Public&nbSp;static void main (String[] args) {local local = new local (New Remote ( ), "Hello"); Local.sendmessage ();} Public void run () {remote.executemessage (message, this);}}
C, recipients of remote messages
/** * This class is equivalent to your classmates */public class Remote { /** * Handling Message * @ Messages received by param msg * @param callBack Callback function handling classes */ public void ExecuteMessage (String msg,callback callback) { /** analog remote classes are dealing with other things that may take a lot of time **/ for (int i=0;i<1000000000;i++) { } / * * deal with other things and now deal with messages **/&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (msg); system.out.println ("I hava executed the message by local "); /** execution Callback **/ callback.execute (new string[]{"nice to meet you~!"}); //This is equivalent to the students after the execution of the call you } }
One of the benefits of callbacks: It's easier and more flexible to abandon the callback method that inherits the abstract class way
From http://hellosure.iteye.com/blog/1130176
Public abstract class b{ public void execute () { getconnection (); docrud (); releaseconnection (); } public abstract void Docrud (); public void getconnection () { system.out.println ("Get Connected ..."); } public void releaseconnection () { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Release connection ..."); } }public class A extends B{ Public void docrud () { System.out.println ("Perform add Operation ..."); } public void add () { docrud (); } }public Class c extends b{ public void docrud () { system.out.println ("Perform delete operation ..."); } public void delete () { &nbsP; docrud (); } }
interface CallBack{ public void docrud (); } public class HibernateTemplate { public void execute (CallBack action) { getconnection (); action.docrud (); releaseconnection (); } public void add () { &nBsp; execute (New callback () { public void docrud () { system.out.println (" Perform the add Operation ... "); } }); } public void delete () { execute (New callback () { public void docrud () { System.out.println ("Perform delete operation ..."); } }); } public void getconnection () { system.out.println ("Get Connected ... "); } public void Releaseconnection () { SYSTEM.OUT.PRINTLN ("Release connection ..."); } }