Java

Blocking or Non-Blocking, Synchronous and Asynchronous

RyanGomdoriPooh 2021. 10. 10. 15:38

Overview

When we develop application, we always think concepts called "Blocking, Non-blocking, Synchronous, Asynchronous".

We usually think that "Blocking, non-blocking, Synchronous, Asynchronous" are the same.

But, This perspective is wrong. It is unrelated concepts each other. It is difficult and confusing concepts.

It is confused between "Blocking" and "Synchronous". Likewise, It is confused between "Non-blocking" and "Asynchronous".

 

"Synchronous and Asynchronous" must have at least two subjects performing the task.
At this time, if the (start, end) time of work is aligned with each other, it is called "Synchronous", and if the time of work is not related to each other, it is called "Asynchronous".

On the other hand, "Blocking and Non-blocking" requires at least two targets for work.
Because the two concepts have different perspectives, various combinations of "synchronous/blocking", "synchronous/non-blocking", "asynchronous/blocking", and "asynchronous/non-blocking" are possible.

 

Then, let's look at details of the contents now.

 


Index

  1. Concept of Blocking and Non-blocking
  2. Concept of Sync and Async
  3. Difference between "Blocking and Non-blocking" and "Sync and Async"
  4. Cases: (Blocking or Non-blocking) X (Sync or Async)

 


1. Blocking and Non-blocking

In blocking and non-blocking, it is important how to deal with the subject who performs other tasks.

If you do your own work, it waits from the beginning to the end of the work done by another person, and start your work again, this is blocking.

If you continue your work regardless of the work of other subjects, this is non-blocking.

Thread A is calling another target to do something, receiving the results from it, and resuming the operation.

For example, Java can use JDBC to ask DB questions and get results called blocking operations.

On the contrary, if you ask another subject to work and do your own work without waiting for the results to be received, this can be called non-blocking.

 

Blocking and Non-blocking are mainly used in reading and writing IO.

Blocking

- It ends when you get the return value.

- It continues to wait until the requested operation is completed.
- Until the requested task is completed, the thread waits and continues to use/stand by one thread until the return value is received.

Non-blocking

- Although the requested work cannot be completed immediately, return it immediately.
- Do not return immediately. It prevents you from working.
- One thread can handle multiple IOs by non-blocking.

 

2. Synchronous and Asynchronous

When two or more subjects is executing for task, task of each subject finishes at the same time or starts at the same time.

Or, If you start and end at the same time, or if another subject starts working at the same time as one task.

It can be seen as a synchronous task.

Asynchronous work refers to when the two subjects have a separate start/end time regardless of each other's start or end time.

It can be called asynchronous when the work done by different subjects has nothing to do with their work start or end time.

Synchronous

- Thread1 started working and waited until Task1 was over. And then It starts Task2.
- When a work is requested, the result value of the request is directly returned.
- The result value of the request is the same as the return value.
- The called function cares about the completion of the task.

Asynchronous

- Thread1 can start work, not wait for completion, and Thread1 can handle other tasks.
- When a work is requested, the result value of the request is indirectly received.
- The result value of the request may be different from the return value.
- The request operation is executed on another thread.
- Processing through callback is called "asynchronous processing".
- The called function cares about completing the task.

 

3. Difference between "Blocking and Non-blocking" and "Sync and Async"

"When an I/O function is called, it is whether or not to return immediately."
Blocking: Don't return right away
Non-Blocking: Return right away

Blocking

- Until the work of an object that cannot be directly controlled is completed, blocking doesn't hand over control.

- When a calling function requests IO, it means waiting without doing anything until IO processing is completed.

Non-blocking

- Non-Blocking is the opposite concept of blocking.

- It has nothing to do with the work of an object that cannot be directly controlled.

- Regardless of whether IO processing is completed, a function requests I/O and then it can do its other work immediately.

 

"The concern is who cares about the completion of the task of the called I/O function."

Sync: In charge of application
Async: In charge of kernel

Synchronous

- After requesting a task, synchronous task waits for the result of it to come out and process it.
- the process continuously checks the kernel for I/O preparations.

Asynchronous

- If it does not care whether the task is completed, the called function is asynchronous.

- When the end of the previous system call occurs, subsequent processing is performed.

 

3. Cases: (Blocking or Non-blocking) X (Sync or Async)

In this step, Explain the combination of (blocking, non-blocking) and (sync, async).

 

1. Synchronous blocking I/O

2. Synchronous non-blocking I/O

3. Asynchronous blocking I/O

4. Asynchronous Non-Blocking I/O (AIO)

 

Synchronous blocking I/O

It is the most basic I/O model, which refers to I/O system calls such as read and write files in general.

If there is no special setting, it operates as blocking.


After the user process calls read(), the kernel does not return until the data is copied to the user buffer

and the user process waits while stopping its work. (Waste of resources such as CPU)


Therefore, if a server to which multiple clients is connected is implemented in a blocking manner, if one client proceeds with an I/O operation, the process (or thread) will be stopped, so each client must create and connect a process (or thread).

However, as the number of users increases, the process (or thread) increases.

 

examples)

- query to DB by JDBC

- In method, call another method to get result immediately

Synchronous non-blocking I/O

The kernel reads the data, stores it in a buffer, and copies the content to the user.

Since the buffer is loaded into the memory held by the kernel, inter-memory copying occurs and can be received at a higher speed than I/O.

However, if the buffer is empty, the kernel immediately returns an error (EWOULDBLOCK), causing the user process to continuously call read() to check the I/O readiness.

Since system calls occur repeatedly (polling method), resources such as cpu are wasted.

 

examples)

- Polling

Asynchronous blocking I/O

It is used for the purpose of multiplexing I/O using system calls such as select or poll.

Select and poll may perform an inspection to see if data is prepared in multiple descriptors and return to process multiple I/O if a prepared descriptor is found.

 

However, the selection is limited to the number of management file descriptors and poll is not limited, but the size of the check mask per file descriptor is large, and thus performance degrades when the number of connectors increases.

 

"Asynchronous blocking" combinations eventually have to wait for other tasks to be completed, so they have similar work efficiency to "Synchronous blocking", that is, they don't have very good efficiency.

"Synchronous blocking" is not the situation the developers intended. They performed "Asynchronous, non-blocking" work, but this result comes out when they performed the blocking work without even realizing it.

 

examples)

- Asynchronous, non-blocking operations are called, and when you try to check the result value of the called operation while working on your own (blocking method is executed)

Asynchronous Non-Blocking I/O (AIO)

An I/O model that responds to a result at a time when I/O processing is completed is referred to as asynchronous I/O.

The asynchronous I/O reply is made in the form of a signal or callback, and the application may proceed with other tasks until a reply is made.

The process is the same as non-blocking I/O in that it is not in a block state, but non-blocking I/O notifies when the I/O process is completed, while non-blocking I/O judges the I/O as an error.

 

examples)

- Case consists of divided logic by a signal or callback

 


Conclusion

Let's talk about it briefly.

Between application and kernel, it is how to act called logic.

You read the details and the graphs for the above cases, you will understand.
The important thing here is to look at the application and kernel areas separately.


Reference

https://en.wikipedia.org/wiki/Asynchronous_I/O

 

Asynchronous I/O - Wikipedia

In computer science, asynchronous I/O (also non-sequential I/O) is a form of input/output processing that permits other processing to continue before the transmission has finished. Input and output (I/O) operations on a computer can be extremely slow compa

en.wikipedia.org

https://www.tornadoweb.org/en/stable/guide/async.html

 

Asynchronous and non-Blocking I/O — Tornado 6.1 documentation

Real-time web features require a long-lived mostly-idle connection per user. In a traditional synchronous web server, this implies devoting one thread to each user, which can be very expensive. To minimize the cost of concurrent connections, Tornado uses a

www.tornadoweb.org

https://stackoverflow.com/questions/2625493/asynchronous-and-non-blocking-calls-also-between-blocking-and-synchronous

 

asynchronous and non-blocking calls? also between blocking and synchronous

What is the difference between asynchronous and non-blocking calls? Also between blocking and synchronous calls (with examples please)?

stackoverflow.com