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
- /**
- * 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 Execute (Object ... objects);
- }
Java is an object-oriented language, so the callback function becomes the callback interface.
B, sender of the message
Java Code
- /**
- * Simple class for sending asynchronous messages locally
- * @author Kook
- *
- */
- public class Local implements callback,runnable{
- /**
- * The class that receives the message remotely, simulates point-to-point
- */
- 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);
- }
- }
C, recipients of remote messages
Java Code
- /**
- * Remote class for processing messages
- * @author Kook
- *
- */
- 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~!"});
- }
- }
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