Asynchronous means that the caller does not get the result immediately after the call is made, but instead notifies the caller of the call after the call is made, or processes the call through the callback function.
The callback simply says that there is a in B, so that a is actually called to its own method when it calls a method of B.
Callbacks can be used to implement asynchronous calls to Java, the following example from the Internet.
Callback Interface:
Public Interface CallBack { /** * Executes callback method @param Objects Returns the processed result as a parameter to the callback method * /publicvoid Execute (Object ... objects) ; }
Sender of message:
/*** This class is equivalent to your own*/ Public classLocalImplementscallback,runnable{Privateremote remote; /*** Send out the message*/ PrivateString message; PublicLocal (remote remote, String message) {Super(); This. Remote =remote; This. Message =message; } /*** Send Message*/ Public voidSendMessage () {/**the name of the current thread **/System.out.println (Thread.CurrentThread (). GetName ()); /**Create a new thread to send a message **/Thread Thread=NewThread ( This); Thread.Start (); /**The current thread continues to execute **/System.out.println ("Message has been sent by local~!"); } /*** Callback function after message is sent*/ Public voidExecute (Object ... objects) {/**Print the returned message **/System.out.println (objects[0]); /**Print the name of the thread that sent the message **/System.out.println (Thread.CurrentThread (). GetName ()); /**interrupt the thread that sent the message **/thread.interrupted (); } Public Static voidMain (string[] args) {local local=NewLocal (NewRemote (), "Hello"); Local.sendmessage (); } Public voidrun () {remote.executemessage (message, This);//this is equivalent to call the classmate, after the call, the thread can do other things, just wait until your classmates call you back when you have to respond } }
Recipient of the message:
/*** This class is equivalent to your classmates*/ Public classRemote {/*** Process Messages *@paramMSG received message *@paramCallBack callback function processing class*/ Public voidexecutemessage (String msg,callback CallBack) {/**analog remote classes are dealing with other things that may take a lot of time **/ for(inti=0;i<1000000000;i++) { } /**deal with other things and now deal with the message **/System.out.println (msg); System.out.println ("I Hava executed the message by Local"); /**Execute callback **/Callback.execute (Newstring[]{"Nice to meet you~!"});//that's the equivalent of calling you after your classmates have finished executing . } }
This makes it possible to implement an asynchronous call based on a callback that invokes the method of the message sender when the message recipient finishes processing the task.
asynchronous invocation of a similar implementation, when the picture is loaded slowly, you can load a blurred picture, when the picture is loaded and then go to the local refresh.
Implementing asynchronous calls to Java using callbacks