Java callback interface and java callback

Source: Internet
Author: User

Java callback interface and java callback

 

The purpose of this article is not to introduce the technology used, but to explain the implementation principle of the callback interface.

 

I. asynchronous and synchronous

In other words, asynchronous means that you can continue to execute subsequent actions without waiting for the currently executed actions to be completed.

 

Generally, the execution sequence of a program is: from top to bottom. The subsequent actions must be executed only after the previous actions are completed. This is a concept relative to Asynchronization-synchronization.

 

Case:

A. James called Li Si and asked Li Si to help write the materials.

B. When Li Si received a call, he had his own work to deal with, but he promised Zhang San that after finishing his work, he would help him write the materials and fax them to Zhang San.

C. After the call, Michael will go out for work.

 

Note:

After Mr. Zhang calls Mr. Li and goes out to work, he does not need to wait for Mr. Li to write the materials. Therefore, the messages that James asked Li Si to write materials are asynchronous messages.

On the contrary, if Michael had to wait for Mr. Li to write the materials before he could go out to work, the message would be a synchronous message.

 

Ii. asynchronous implementation

The traditional program executes code one by one from top to bottom.

However, this is not the case in many situations in life. In the above cases, if it takes a few hours for Li Si to help James write the materials, then James must wait for several hours, in this way, James may crash or get mad.

This kind of one-stop processing is not reasonable.

 

You can use the following methods to solve this problem:

Michael asked Mr. Wang to call Mr. Li. After Mr. Li wrote the materials, Mr. Wang handed over the materials to Mr. Zhang. In this way, James can go out to do other things.

The problem was solved reasonably. In the past, the work of Zhang San was completed by Zhang San and Wang Wu. The work was carried out simultaneously on both sides without delay.

 

Iii. Implementation of computer languages

There is a solution. How can we use a program to simulate the implementation?

 

A. For jobs previously handled by A thread, you can add A new thread to achieve asynchronous purposes. This is the multithreading technology in JAVA.

B. Finally, the materials written by Li Si must be handed over to Zhang San for him to use. This is the callback.

 

You can understand the callback as follows:

A sends A message to B. B processes the request of A and returns the result to A. A then performs further processing on the result returned by B.

 

Iv. Java code implementation

A,Callback implementation

 

1/** 2 * CallBack interface 3 * @ author rhythm 4*5 */6 public interface CallBack {7/** 8 * execute CallBack Method 9 * @ param objects will process the result is returned to the callback Method 10 */11 public void execute (Object... objects); 12}

  

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

 

B,Message sender

1/** 2 * simple class for sending asynchronous messages locally 3 * @ author rhythm 4*5 */6 public class Local implements CallBack, runnable {7 8/** 9 * Remote message receiving Class, simulating point-to-point10 */11 private remote; 12 13/** 14 * sent messages 15 */16 private String message; 17 18 public Local (Remote remote, String message) {19 super (); 20 this. remote = remote; 21 this. message = message; 22} 23 24/** 25 * Send message 26 */27 public void sendMessage () {28/** name of the current thread * */29 System. out. println (Thread. currentThread (). getName (); 30/** create a new Thread to send messages **/31 thread Thread = new thread (this); 32 Thread. start (); 33/** the current thread continues to execute **/34 System. out. println ("Message has been sent by Local ~! "); 35} 36 37/** 38 * callback function 39 */40 public void execute (Object... objects) {41/** print the returned message **/42 System. out. println (objects [0]); 43/** print the name of the message sending thread **/44 System. out. println (Thread. currentThread (). getName (); 45/** Thread that interrupts message sending **/46 Thread. interrupted (); 47} 48 49 public static void main (String [] args) {50 Local = new local (new Remote (), "Hello"); 51 52 Local. sendMessage (); 53} 54 55 public void run () {56 remote.exe cuteMessage (message, this); 57 58} 59}

 

C,Remote message recipient

 

1/** 2 * Remote class for processing messages 3 * @ author rhythm 4*5 */6 public class Remote {7 8/** 9 * processing messages 10 * @ param msg received message 11 * @ param callBack function processing class 12 */13 public void executeMessage (String msg, callBack callBack) {14/** simulate that the remote class is processing other tasks. It may take a lot of time **/15 for (int I = 0; I <1000000000; I ++) {16 17} 18/** after processing other tasks, process the message now. **/19 System. out. println (msg); 20 System. out. println ("I hava executed the message by Local"); 21/** execution callback **/22 CallBack.exe cute (new String [] {"Nice to meet you ~! "}); 23} 24 25}

  

  

  

The executeMessage () method needs to receive a message parameter, indicating the sent message. The CallBack parameter is his own, that is, this here. After a message is sentLocalClass to process the message, and call its own execute method to process the message result.

If this is not used here, but the implementation class of other CallBack interfaces, it cannot be called a "CallBack". In the OO world, it is a "delegate ". That is to say:The "Callback" must be the message sender to process the message result. Otherwise, it cannot be called a callback.. This concept must be clear.

 

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.