Java IO 模型 (BIO NIO, AIO)

BIO

  • A traditional IO model, the blocking phenomenon will occur in the process of reading and writing data. When a user thread issues an IO request, the kernel will go to see if the data is ready , if not ready to wait for the data will be ready, but the user will thread is blocked , the user thread to hand over the CPU. When the data is ready, the kernel will copy the data to the user thread, thread and return the results to the user, the user thread before lifting block state. Examples of typical blocking IO model is: data = socket.read (); if the data is not ready, it will have been blocked in the read method.

NIO

  • Java NIO actually multiplexed IO. In multiplexed IO model, there is a thread continued to poll the status of multiple socket only when the socket read and write real event, it really calls the actual IO read and write operations .
  • Because multiplexing IO model, only need to use one thread can manage multiple socket , the system does not need to create a new process or thread, there is no need to maintain these threads and processes, and only in the real socket read event when, IO resources will be used, so it significantly reduces resource consumption.
  • In the Java NIO, by selector.select () to query each channel if an event has to reach, if not an event, it has been blocked in there , so it may lead to blocking the user thread.
  • Once the event in response to a large body, it will lead to subsequent delays in processing the event, and the event will affect the new poll.
    Here Insert Picture Description

AIO

  • In asynchronous IO model, after you read the thread to initiate operation, you can immediately start to do other things. On the other hand, from the kernel's point of view, when it is subjected to a asynchronous read, it will return immediately, indicating that read request has been successfully launched, it will not block any user thread
  • Just initiate a first request indicating IO operation has been completed, can be used directly to data reception success signal when the core is returned.
  • Said asynchronous IO model, the two phases of IO operations will not block user thread, these two phases are done automatically by the kernel and then sends a signal to inform the user thread operation has been completed, received signal indicates IO operation has been completed , you do not need to call the function in the user IO threads actual read and write operations.
Published 20 original articles · won praise 0 · Views 256

Guess you like

Origin blog.csdn.net/white_zzZ/article/details/103941538