Java simulates the sending and callback of asynchronous messages

Source: Internet
Author: User

http://kt8668.iteye.com/blog/205739

The purpose of this article is not to introduce what technology to use, but to focus on its implementation principles.

One, asynchronous and synchronous

Speaking of the popular point, asynchronous is not necessary to wait for the current execution of the completion of the action, you can continue to perform the subsequent actions.

Typically, the order in which a program executes is: From top to bottom, execution in turn. The subsequent action must wait for the previous action to complete before executing. This is a concept that is relative to asynchrony-synchronous.

Case:

A, Zhang San call John Doe, let John Doe help write a material.

B, John Doe received the phone when the hand has his own work to deal with, but he promised Zhang San, busy hand after work immediately help Zhang San write good material, and fax to Zhang San.

C, after the phone, Zhang San out of office.

Description

Zhang San to Lee four through the telephone, went out to work, he does not need to wait for John Doe to write the material well before going out. Then the message that Zhang San let John Doe write material belongs to the asynchronous message.

On the contrary, if the Zhang San must wait for John Doe to write the material in order to go out to work, then this message belongs to the synchronization message.

Second, the realization of asynchronous

Traditional program execution code is executed from top to bottom, one at a line.

But there are many things in life is not so, in the above case, if John Doe need a few hours later to help Zhang San write good material, the three must wait a few hours, so Zhang San may collapse or crazy.

Such a dragon-like treatment, the display is not very reasonable.

You can use the following methods to handle this problem:

Zhang San find Harry to John Doe call, wait for Li four write good material, from Harry to Zhang San. So Zhang San can go out and do other things.

The problem has been resolved reasonably, before Zhang 31 line of work, by Zhang San and Wang 52 line to complete, both sides at the same time, do not delay each other.

Third, the realization of computer language

How to use the program to simulate the implementation?

A, work that was previously handled by a thread can be asynchronous by adding a new thread. This is the multithreading technique in Java.

B, the last John Doe written material must be handed to Zhang San, to do his use. This is the callback.

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.

Iv. implementation of Java code

A, implementation of callbacks

Java code
    1. /**
    2. * Callback Interface
    3. * @author Kook
    4. *
    5. */
    6. Public interface CallBack {
    7. /**
    8. * Execute callback method
    9. * @param objects returns the processed result as a parameter to the callback method
    10. */
    11. public void Execute (Object ... objects);
    12. }

Java is an object-oriented language, so the callback function becomes the callback interface.

B, sender of the message

Java Code
  1. /**
  2. * Simple class for sending asynchronous messages locally
  3. * @author Kook
  4. *
  5. */
  6. public class Local implements callback,runnable{
  7. /**
  8. * The class that receives the message remotely, simulates point-to-point
  9. */
  10. private remote remote;
  11. /**
  12. * Send out the message
  13. */
  14. Private String message;
  15. Public Local (remote remote, String message) {
  16. Super ();
  17. This.remote = remote;
  18. this.message = message;
  19. }
  20. /**
  21. * Send Message
  22. */
  23. public void SendMessage ()
  24. {
  25. /** the name of the current thread **/
  26. System.out.println (Thread.CurrentThread (). GetName ());
  27. /** Create a new thread to send the message **/
  28. Thread thread = new thread (this);
  29. Thread.Start ();
  30. /** The current thread continues execution **/
  31. System.out.println ("Message had been sent by local~!");
  32. }
  33. /**
  34. * Callback function after message is sent
  35. */
  36. public void Execute (Object ... objects) {
  37. /** prints the returned message **/
  38. System.out.println (Objects[0]);
  39. /** Print the thread name of the sending message **/
  40. System.out.println (Thread.CurrentThread (). GetName ());
  41. /** interrupts the thread that sent the message **/
  42. Thread.interrupted ();
  43. }
  44. public static void Main (string[] args)
  45. {
  46. Local local = new local (new Remote (), "Hello");
  47. Local.sendmessage ();
  48. }
  49. public void Run () {
  50. Remote.executemessage (message, this);
  51. }
  52. }

C, recipients of remote messages

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

Executes the main method of the Local class.

Note The row for the red background in the Local class:

Remote.executemessage (Message, this);

The ExecuteMessage method needs to receive a message parameter, which means that the messages are sent out, while the callback parameter is himself, that is, this. Indicates that the message is sent, handled by the Local class itself, and calls its own Execute method to process the message result.

If this is not used here, but with other implementations of the callback interface, then it can not be called "callback", in the OO world, it belongs to "delegation". Other words "Callback" must be the sender of the message to process the result of the message, otherwise it cannot be called a callback。 The concept must be clear.

Java simulates the sending and callback of asynchronous messages

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.