Java nio foundation USES examples

  • 2020-04-01 02:25:30
  • OfStack

The technology presented in jdk1.4, non-blocking IO, USES event-based processing.
Traditional IO techniques are blocked, such as reading a file, and the read method is blocked until data is read in.
Summarized as:
1. Java IO is blocked. After opening an IO channel, read will wait to read the byte content at the port side.
2, on the basis of 1 to improve to, opened the thread, serversocker.accept() after the thread to wait, but when the concurrency is high, it is quite expensive.
3. Java nio is non-blocking, and adopts the reactor mode, or observer observer mode, to monitor the IO port, register the event to the selector, and trigger the behavior when the event meets the condition.
The general process is as follows:
Java nio provides a selector, which is similar to an observer that registers a socketchannel that needs to be probed into into a selector
And then we do something else, and when something happens, the selector notifies us, returns a set of selectionkeys, and we read those keys and we get the socketchannel that we just registered, and we read the data from that channel and we process the business logic.
The internal principle of selector is to poll through the registered channel to determine whether the event registered by the channel has occurred.
Examples of the code are:


//1. Create a selector object
Selector selector = Selector.open();
//2. Set up the channel object and bind it to port 8080
ServerSocketChannel ssc = ServerSocketChannel.open();
InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(),8080);
ssc.socket().bind(address);
//3. Set the channel to be non-blocking
ssc.configureBlocking(false);
//Register channels with selectors and the events we're interested in
SelectionKey skey = ssc.register(selector,SelectionKey.OP_ACCEPT);//Accept is registered here, and the server receives the client connection event
//4. Simply simulate the next polling process
while(true)
{
  //Selector notifies us that an event of interest has occurred through the select method
  int nKeys = selector.select();
  //When nKeys> Zero means that the event happened
  //You can then go through selecter.selectedkeys (); Method gets the key set
  Set selectKeys = selector.selectedKeys();
  //5. Iterate over the keys object, and do the adaptation business logic processing respectively
  //Such as:
  s = (SelectionKey)(selectKeys.iterator()).next();
  if(s.isAcceptable())//Determines the OP_ACCEPT event for registration
  { 
    //Get the channel we just registered from the channel
    Socket socket = ((ServerSocketChannel)s.channel()).accept().socket();
    SocketChannel sc = socket.getChannel();
    //Set to non-blocking
    sc.configureBlocking(false);
  //Register the read/write event
    sc.register(selector, SelectionKey.OP_READ |SelectionKey.OP_WRITE);
  }
}
//Above is the pseudocode for a simple Java nio demonstration
//At this point, the client side can Telnet hostname port number to connect to the server server.


Related articles: