Java NIO (i) I/O model overview

Source: Internet
Author: User

Basic concept to tellWhat is synchronization?

Synchronization is: If there are multiple tasks or events to occur, these tasks or events must be done individually, an event or the execution of a task will cause the entire process of temporary wait, these events can not be executed concurrently.

What is async?

Async is: If there are multiple tasks or events occur, these events can be executed concurrently, an event or task execution will not cause the entire process of temporary waiting

For a simple example, if there is a task that includes two subtasks A and B, for synchronization, when a is in the process of execution, B waits until a executes, B can execute, and for async A and B are executed concurrently, B does not have to wait for a to execute, This will not result in a temporary wait for the entire task due to the execution of a.

Focus:
Whether it is synchronous or asynchronous to see if the occurrence or execution of an event causes a temporary wait for the entire process when multiple tasks and events occur
If the whole process is caused by a temporary wait, it is synchronous;
If it can be executed in parallel, it is asynchronous.

code example

Synchronous code example

void fun1 () {        }    void  fun2 () {        }     void  function () {    fun1 ();     Fun2 ();     .....     ..... }

Explanation: This code is a typical synchronization, in the method function, fun1 in the execution of the process will cause the subsequent fun2 can not be executed, fun2 must wait for fun1 execution to execute.

Async code Example

void fun1 () {       }   void  fun2 () {       }   void  function () {      New Thread () {         publicvoid  run () {             fun1 ();         }     }. Start ();            New Thread () {         publicvoid  run () {             fun2 ();         }     }. Start ();      .....     ..... }

Explanation: This code is a typical asynchronous, fun1 execution does not affect the execution of fun2, and fun1 and fun2 execution will not cause its subsequent execution of the process is temporarily waiting.

what is blocking?

Blocking is: When an event or task is executing, it makes a request operation, but because the requested action requires a condition that is not met, it waits until the condition is met.

What is non-blocking?

Non-blocking is: When an event or task is executing, it issues a request action, and if the requested action requires a condition that is not met, it returns a flag message that the condition is not met and will not wait there.

To give a simple example:
If I want to read the contents of a file, if there is no content in the file readable, for the block will be waiting until the contents of the file can be read;
For non-blocking, a flag message is returned to inform the file that there is no content to read.

This is the difference between blocking and non-blocking. That is, the difference between blocking and non-blocking is that when a request is made, if the condition is not met, it waits or returns a flag message.

Synchronous and asynchronous focus on the execution of multiple tasks, whether the execution of a task will lead to a temporary wait for the whole process;

While blocking and non-blocking focuses on issuing a request operation, if the condition of the operation does not meet whether it will return a flag message that the condition is not satisfied.

Java NIO (i) I/O model overview

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.