Java BIO / NIO / AIO learning

0 Preface

  This paper describes the Java language IO related presentations.

  The description can be summed up by the basic 10 words, it is the purpose of synchronous and asynchronous, non-blocking is blocking and implementation.

  1. Synchronization: user process triggered IO operations and wait to see or polling IO operation is ready . ( Their street to buy clothes, personally doing it, something else can not do )
  2. Asynchronous: IO operation triggered after a user process began to do their own thing, and when IO operation has been completed will be notified IO completion. ( Tell their friends the right size clothes, size, color, make friends commissioned to sell, then they can shop and go to something else )
  3. Blocking: When attempting to read and write to the file descriptor, if things did not readable, or is temporarily unavailable written, the program goes to a waiting state until there is something to read or write so far. ( To the bus station to recharge and found this time, recharge were absent (probably went to the toilet), and then we'll wait here, wait until the recharge comes home so far )
  4. Nonblocking: non-blocking state, if not something to read, or not to write, read and write function returns immediately, without waiting. ( Go ordering, queuing for small ticket, you can receive after we play phone, or chat with other people, when we round, the speaker will notify the bank )

1 from the programming language level

  BIO: synchronous blocking IO, a connecting thread

  NIO: synchronous non-blocking IO, a request for a thread

  AIO: asynchronous non-blocking IO, a valid request a thread

2 BIO

  Before JDK1.4, written in Java network request, is to create a ServerSocket, then the client will be asked if there is a thread can handle establishing Socket, if not, either waiting or rejected. Namely: a connection request process corresponds to a thread Server.

3 NIO

  Provided in JDK1.4 and later a set of API to specifically operate non-blocking I / O, we can find the classes and interfaces in java.nio package and its subpackages. Because this API is I / O API JDK new offer, therefore, also called the New I / O, this is the package name nio origin. This API consists of three major components: Buffer (Buffers), channels (Channels) and non-blocking I / O core classes.

  NIO based Reactor, when the socket has a socket stream read or write, the operating system notifies the corresponding reference program for processing, and then applied to the stream buffer to read or write operation system. 
That is, this time, is not a connection must correspond to a processing thread, but rather valid request, corresponds to a thread, when no data connection is not working threads to handle.

4 AIO

  In JDK1.7, this part is referred NIO.2, the following four major increase in java.nio.channels package asynchronous channel, wherein the read / write method, the object returns a callback function, when after performing read / write operations, directly call the callback function.

 

4.1 AIO implementation

  On Windows, AIO achieved by IOCP is accomplished , look at the source code JDK can be found

WindowsAsynchronousSocketChannelImpl

  Look implement an interface:

implements Iocp.OverlappedChannel

  On linux, achieve AIO is done by epoll , see JDK source code can be found to achieve the source code is:

UnixAsynchronousSocketChannelImpl

  Look implement an interface:

implements Port.PollableChannel

  This is the biggest difference with the windows, to achieve the poll, after linux2.6, use epoll default.

Guess you like

Origin www.cnblogs.com/huanghzm/p/11024931.html