Java callback mechanism in layman's

Source: Internet
Author: User

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
    1. Public interface CallBack {
    2. /**  
    3. * Execute callback method
    4. * @param objects returns the processed result as a parameter to the callback method
    5. */
    6. public void Execute (Object ... objects);
    7. }

Sender of message:

Java code
  1. /**
  2. * This class is equivalent to your own
  3. */
  4. Public class Local implements callback,runnable{
  5. private remote remote;
  6. /**  
  7. * Send out the message
  8. */
  9. private String message;
  10. Public Local (remote remote, String message) {
  11. super ();
  12. this.remote = remote;
  13. this.message = message;
  14. }
  15. /**  
  16. * Send Message
  17. */
  18. public void SendMessage ()
  19. {
  20. /** The name of the current thread **/
  21. System.out.println (Thread.CurrentThread (). GetName ());
  22. /** Create a new thread to send the message **/
  23. Thread thread = new Thread (this);
  24. Thread.Start ();
  25. /** The current thread continues execution **/
  26. System.out.println ("Message had been sent by local~!");
  27. }
  28. /**  
  29. * Callback function after message is sent
  30. */
  31. public void Execute (Object ... objects) {
  32. /** Prints the returned message **/
  33. System.out.println (objects[0]);
  34. /** Print the thread name of the sending message **/
  35. System.out.println (Thread.CurrentThread (). GetName ());
  36. /** interrupts the thread that sent the message **/
  37. Thread.interrupted ();
  38. }
  39. public static void Main (string[] args)
  40. {
  41. Local local = new local (new Remote (),"Hello");
  42. Local.sendmessage ();
  43. }
  44. public Void Run () {
  45. 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
  46. }
  47. }

Recipient of the message:

Java code
  1. /**
  2. * This class is equivalent to your classmates
  3. */
  4. Public class Remote {
  5. /**  
  6. * Processing messages
  7. * Messages received by @param msg
  8. * @param callBack callback function processing class
  9. */
  10. public void ExecuteMessage (String msg,callback CallBack)
  11. {
  12. /** analog remote classes are dealing with other things that may take a lot of time **/
  13. For (int i=0;i<1000000000;i++)
  14. {
  15. }
  16. /** deal with other things, now to deal with the message **/
  17. SYSTEM.OUT.PRINTLN (msg);
  18. System.out.println ("I Hava executed the message by Local");
  19. /** Execution Callback **/
  20. Callback.execute (new string[]{"Nice to meet you~!"}); //This is equivalent to calling you after your classmates have finished executing
  21. }
  22. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.