On the internet to see a metaphor, feel very image, here to borrow:
You have a complex problem to solve, call your classmates, your classmates said can solve the problem, but it takes some time, then you can't always hold the phone there waiting, you will tell him your phone number, let him solve after the call to inform you. The callback is reflected in your classmate and in turn dial your number.
Combined to the previous analysis, you called your classmate is "You call me", your classmates after the completion of the call to you is "I called You back."
Here's a good example of using callbacks with asynchronous invocations , from the http://kt8668.iteye.com/blog/205739 callback interface:
Java code
- Public interface CallBack {
- /**
- * Execute callback method
- * @param objects returns the processed result as a parameter to the callback method
- */
- public void Execute (Object ... objects);
- }
Sender of message:
Java code
- /**
- * This class is equivalent to your own
- */
- Public class Local implements callback,runnable{
- private remote remote;
- /**
- * Send out the message
- */
- private String message;
- Public Local (remote remote, String message) {
- super ();
- this.remote = remote;
- this.message = message;
- }
- /**
- * Send 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 had been sent by local~!");
- }
- /**
- * Callback function after message is sent
- */
- public void Execute (Object ... objects) {
- /** Prints the returned message **/
- System.out.println (objects[0]);
- /** Print the thread name of the sending message **/
- System.out.println (Thread.CurrentThread (). GetName ());
- /** interrupts the thread that sent the message **/
- Thread.interrupted ();
- }
- public static void Main (string[] args)
- {
- Local local = new local (new Remote (),"Hello");
- Local.sendmessage ();
- }
- public Void Run () {
- Remote.executemessage (message, this ); //This is equivalent to calling 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:
Java code
- /**
- * This class is equivalent to your classmates
- */
- Public class Remote {
- /**
- * Processing messages
- * Messages received by @param msg
- * @param callBack callback function processing class
- */
- 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, now to deal with the message **/
- 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 calling you after your classmates have finished executing
- }
- }
As the above example shows, callbacks can be used as the basis of an asynchronous invocation to implement an asynchronous invocation.
Java callback mechanism in layman's