Java synchronous and asynchronous-blocking and nonblocking understanding

Source: Internet
Author: User

Java synchronous and asynchronous-blocking and nonblocking understanding

Synchronous and asynchronous in Java, blocking and non-blocking are all used to describe interactions, except that they describe two different levels of interaction.

Synchronous vs. asynchronous

Synchronous and asynchronous pay more attention to whether both sides of the interaction can work at the same time. Completing a task synchronously means that the order of completion of multiple tasks is serial, assuming that task a relies on task B, and that task a must wait until task B is complete before it can continue, and that the execution of the process for a->b; to complete the task asynchronously means that the completion of multiple tasks can be parallel, This situation is more applicable to the absence of causality between tasks, if task A is required to perform task B, and task A's completion does not depend on the results of task B, then task a invokes task B can continue to follow the next steps without waiting for task B to complete, and does not care whether task B is completed, at this time, task A and any Service B is parallel.

To deepen your understanding of synchronization and Asynchrony, you can use the categories of calls and text messages to interact synchronously and asynchronously. When you make a call, one side's follow-up must wait until the other party finishes, and this interaction is synchronous. Texting means we don't care about the outcome of the message, we care if we send a text message, we can go on the job at hand, this interaction is asynchronous.

Blocking and non-blocking

Blocking and non-blocking concerns whether the interaction between the two sides can work flexibly. If object A is interacting with object B, and object B needs to think about a problem for a while to reply to a, then object A can choose to wait for object B to reply, which is a blocking interaction, while object A can choose to do other work at object B's time of thinking and wait until object B finishes It is a kind of non-blocking interaction to make subsequent interactions after thinking.

In general, blocking and non-blocking are used to describe CPU consumption. We stop the CPU and wait until the slow operation is finished, then the work is called blocking, and the CPU completes the other work before the slow operation is completed, and then the work is called non-blocking until the slow operation is complete.

Java synchronous and asynchronous-blocking and nonblocking understanding

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.