Java asynchronous callback and asynchronous callback

Source: Internet
Author: User

Java asynchronous callback and asynchronous callback

Author: Chan Lou wangyue (http://www.cnblogs.com/yaoyinglong)

1. Start telling stories:

It was lunch, but the weather was too cold to come up with the office door. So you dialed the order number of XXX hotel, "Hello! Hello, I am a side dish from XXX company. I 'd like to know more ......". Then I continued to do your work. After a while, "Hello, it's a little dish from XXX company. It's your lunch ". This process is a typical asynchronous callback. Let's take a look at what are the necessary conditions:

Both parties must comply with the agreement. If either party fails to comply with the agreement, this work will not be done.

First, we define a business agreement for food delivery:

1 public interface SendFood {2 void sendFood (ReceiveFood receive, String foodName); 3}View Code

Then we define an agreement on where to deliver the food:

1 public interface ReceiveFood {2 void receiveFood (Food food); 3}View Code

Then we define a restaurant to keep it compliant with the food delivery agreement:

1 public class XXXRestaurant implements SendFood {2 3 @ Override 4 public void sendFood (ReceiveFood receive, String foodName) {5 // tell the kitchen to cook ...... 6 Food lunch = cook (foodName); 7 // deliver the lunch to your agreed location 8 receive. receiveFood (lunch); 9} 10 private Food cook (String foodName) {11 // after a period of time, some foods have come up with 12 Food someFood = new Food (foodName ); 13 return someFood; 14} 15}View Code

Then we define ourselves, and we follow the agreement to accept meals:

1 public class XiaoCai implements ReceiveFood {2 private SendFood restaurant; 3 public XiaoCai (SendFood restaurant) {4 this. restaurant = restaurant; 5} 6 public void orderFood (final String foodName) {7 new Runnable () {8 @ Override 9 public void run () {10 restaurant. sendFood (XiaoCai. this, foodName); 11} 12 }. run (); 13} 14 @ Override15 public void receiveFood (Food food) {16 System. out. println ("ready"); 17} 18}View Code

Test:

 

2. Understanding

Therefore, we can understand the callback as follows: A entrusts B to do one thing, and then B notifies A after it completes. It is manifested in the Code: The method fa of Class A calls the method fb of Class B, and the method fa1 of Class A is called in fb after meals.

3. Differences between multithreading and Asynchronization [reprint]

Thank you very much.YDHL iPhone DevContribution

3.1 nature of asynchronous operations

All programs will eventually be executed by computer hardware. Therefore, to better understand the nature of asynchronous operations, we need to understand its hardware base. Friends who are familiar with computer hardware must be familiar with the DMA term. The technical specifications of hard disks and optical drives all have clear DMA mode indicators. In fact, NICS, sound cards, and video cards also have DMA functions. DMA means direct memory access. That is to say, hardware with DMA functions can exchange data with memory without consuming CPU resources. As long as the CPU sends a command when initiating data transmission, the hardware starts to exchange data with the memory. After the transmission is complete, the hardware triggers an interruption to notify the operation to complete. These I/O operations that do not consume CPU time are the hardware basis of asynchronous operations. Therefore, asynchronous DMA operations can be initiated even in a single process (and wireless process) system such as DOS.

3.2 thread nature

A thread is not a computer hardware function, but a logic function provided by the operating system. A thread is essentially a piece of code that runs concurrently in a process, therefore, threads require the operating system to invest CPU resources for running and scheduling.

3.3 Advantages and Disadvantages of asynchronous operations

Because asynchronous operations do not require additional thread burden and Use callback for processing, the processing function does not need to use shared variables (even if it cannot be completely used, at least the number of shared variables can be reduced) to reduce the possibility of deadlocks. Of course, asynchronous operations are not perfect. It is highly complex to write asynchronous operations. The program mainly uses the callback method for processing, which is a bit difficult to debug with the way of thinking of ordinary people.

3.4 Advantages and Disadvantages of Multithreading

The advantages of multithreading are obvious. The processing program in the thread is still executed in sequence, which conforms to the thinking habits of ordinary people, So programming is simple. However, the disadvantages of multithreading are also obvious. The use (abuse) of threads will impose additional burden on context switching. In addition, shared variables between threads may cause deadlocks.

3.5 Applicability

After understanding the advantages and disadvantages of threads and asynchronous operations, we can discuss the rational use of threads and asynchronous operations. In my opinion, when I/O operations are required, it is more appropriate to use asynchronous operations than to use thread + synchronous I/O operations. I/O operations include not only direct file and network read/write operations, but also cross-process calls such as database operations, Web Service, HttpRequest, And. Net Remoting.
The applicability of threads is those that require long CPU operations, such as time-consuming graphics processing and Algorithm Execution. However, due to the simplicity and habit of using thread programming, many friends often use threads to execute time-consuming I/O operations. In this way, there are only a few concurrent operations, which is not suitable if you need to handle a large number of concurrent operations.

 

Related Article

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.