The difference between

In terms of how it works

  • BIO is flow oriented
  • NIO is buffer – and channel-oriented

BIO in dealing with the client request is the manner in which a blocking type (request to within the timeout if didn’t get the result has been there) opened another thread out service for the client, how many clients have how many threads, and thread in a computer is a very precious resource, and a thread take up at least 1 m memory space, This means that one gigabyte of memory can only handle 1000 requests, which is obviously a waste of resources. NIO is buffer-oriented and can handle requests from multiple clients in one thread, which greatly increases thread utilization. So just to illustrate the concept of blocking and non-blocking and synchronous asynchronism let’s say you go to the mall to buy clothes

  • Synchronous and asynchronous focus on message communication mechanisms

Synchronous: you tell the boss I want to buy li Ning’s sports coat, the boss will go to find, found directly to you asynchronous: you tell the boss I want to buy Li Ning’s sports coat, the boss found a phone call to inform you

  • Blocking and non-blocking are concerned with the state of the program while it waits for the result of the call (message, return value)

Blocking: You tell your boss I want to buy Li Ning sports coats, and do nothing until the boss gives you a result (yes or no), which is the non-blocking of blocking for you: You tell the boss I want to buy a Sports coat from Li Ning. Instead of waiting for the boss to find a coat for you, you go to the supermarket. It’s non-blocking for you

Explanation: In the above scenario, synchronous asynchronous is the boss’s way of informing you, as to what you are going to do during the period (go to the supermarket or wait here silly) the boss does not care; In blocking, you decide whether to wait or go shopping while the boss looks for something. So blocking and non-blocking and synchronous and asynchronous permutation and combination could have four things Is used here to vividly illustrate a scene, if you have an appointment with your girlfriend to go shopping, you need to go to her home waiting at the door, and your girlfriend still make up in the bedroom, at this time, between you and your girlfriend may have a few kinds of such cases

  1. Synchro block: You just stand by the door and do nothing, waiting for your girlfriend to put on makeup and open the door.
  2. Synchronous non blocking: you knock on the door of next door little elder sister, in next door little elder sister’s home play for a while to knock on the door of your girlfriend ask her if she has good, if there is no good to go next door little elder sister’s home play again for a while, and then knock on the door of your girlfriend’s home ask her if she has good…… Until your girlfriend is ready to go shopping.
  3. Asynchronous block: you and your girlfriend agreed, if your girlfriend makeup, call your phone to inform you, and then your girlfriend began to make up, you stand at her door what all don’t do, is silly wait for the phone, until your girlfriend call you say finished, you set out shopping.
  4. Asynchronous non block: you and your girl friend agreed, if your girl friend change good makeup, hit your phone notice you, then your girl friend begin to make up, then you pried open the door of next door little elder sister’s house, play with little elder sister, until your girl friend dozen say to you can walk, you just go with girl friend start shopping.

The third case is rarely used, and the fourth case is used more.

NIO workflow

There are three important components in NIO:

  • The selector — — — — — — — — — — — — — — — — — — — — — — selector
  • The channel — — — — — — — — — — — — — — — — — — — — — —
  • Buffer — — — — — — — — — — — — — — — — — — — — — — — — — buffer

SocketServerChannel: is responsible for binding port and connect a SocketChannel: ServerSocketChannel. The accept ()

  1. The server starts, generating a ServerSocketChannel, which looks to the Selector for the client connection event (OP_accept). Server in place
  2. Client A initiates A connection to the server, and the Selector generates A client connection event that tells the ServerSocketChannel on the server to do A three-way handshake with the client to establish A connection.
  3. After the connection is established, the server starts a SocketChannelA, which looks to the Selector for read events (op_read).
  4. When the client is ready to send the data to the server, the Selector generates a read event, and the Selector realizes that the read event is the one that the SocketChannelA is interested in, notifies the SocketChannelA and passes the data.
  5. When the SocketChannelA receives the data, The server handler reads the data in the Buffer (buffer.read ()) and returns the required data to the client (buffer.write ()).
  6. The SocketChannelA obtains the final data from the server to the client from the Buffer and sends the data to the client (socketchannel.write ()).

A complete client-to-server, server-to-client NIO process roughly ends like this:An important part of this process is the Buffer, which is actually a normal piece of memory for both read and write operations. See the following figure for details: When buffer is in the write stateIn the figure above, position is the position where the data is currently written. Position starts at the top and moves down one space for each block. Capacity refers to the size of the buffer, and limit refers to the bottom.When buffer is in the read stateLimit points to the position originally written, position points to the top, and Capacity remains the largest buffer position. The way to switch between read and write states is to call flip()