The working mechanism of java BIO

Before talking about BIO, first explain the semantics of a few words, such as synchronous and asynchronous, blocking and non-blocking, then what do these words mean?

Synchronization: Synchronization is to do things in the established order, orderly work.

Asynchronous: Asynchronous means doing things without order, as long as they are done.

For example, if you want to eat and then go to the classroom to read, synchronous means that you have to eat first and then read a book. Asynchronous means that you see a lot of people queuing to eat, you go to study first, and come over to eat when there are fewer people in line, or It is reading while queuing to eat.

So what are blocking and non-blocking?

For another example, if you are chatting with a friend on WeChat, you send a message, and your friend does not reply to you, you have not done anything, and have been waiting for a reply. This is blocking. If your friend does not reply to you, you will learn first. It's not just idle waiting, this is non-blocking.

After talking about synchronous and asynchronous, blocking and non-blocking, let's talk about BIO.

BIO: Blocking I/O, which translates to synchronous blocking I/O, that is, each thread can only do its own task when doing tasks, and waits until the other party does not reply. From the definition point of view, the efficiency is too low. If it is such a programmer, it is estimated that the company would have been fired.

So what is the working mechanism?

Then just look at how the data is transmitted. When the server and the client establish a communication link, they are all done through Socket. If the server and the client have established a connection, there will be a Socket instance, each Socket instance There is an inputStream and an outputStream, and when the Socket object is created, the operating system will allocate a buffer area to the inputStream and outputStream, and the server and the client pass the information through the buffer area.


The writing end writes the data to the SendQ queue of the outputStream. When the SendQ is full, the data is transferred to the RecvQ of the inputStream at the other end. If this is the RecvQ in the inputStream is full, then the write method in the outputStream It will be blocked, and when there is enough space in RecvQ, it can continue to receive the transmitted data. Therefore, the size of the cache space is very important. The size of the cache space and the speed of writing and reading will affect the communication link Efficiency, and if both ends transmit data at the same time, then deadlock may occur.

Synchronous blocking means that the client comes to a request, and the server opens a thread for processing. This thread can only process the request before processing other requests. In order to process multiple requests at the same time, only multiple threads can be opened, and each request handles one task. , One-to-one correspondence, the processed thread is destroying the thread. However, the cost of starting and destroying threads is large, so the cost of thread creation and recovery can be reduced by creating a thread pool.

As shown in the figure, the thread pool is used to solve some of the problems of BIO, but there are still problems with BIO in some scenarios. The emergence of NIO has solved many problems caused by BIO. However, the working method of NIO has to wait until the next time. Yes, like friends who like it.

Guess you like

Origin blog.csdn.net/wzs535131/article/details/103724171