We know that the bio of java is a request corresponding to a thread, so can java nio understand that a request corresponds to a channel?

recently, I have been learning about java nio, which has been a bit strange. There is such a common code in nio programming

.
for(xxxxxxx){
   if(selectionKey.isAcceptable()){
       ServerSocketChannel ssc = (ServerSocketChannel) selectionKey.channel();
       SocketChannel socketChannel = ssc.accept();
       socketChannel.configureBlocking(false);
       socketChannel.register(selector,SelectionKey.OP_READ);
    }
    f(selectionKey.isReadable()){
       .......
    }
}

Loop scan the state of the channel. When the channel accept, a new channel socketChannel, will be generated and then the listening event will be specified. Such a tcp connection will form a channel,. Ask whether this is understood

.

it is not feasible for you to understand it only at the code level, you have to combine the code with the network model provided by the operating system to understand it.


1. Bio is a request. A new thread is created to process
2, but nio is not a request corresponding to a channel, but a tcp connection corresponding to a channel.

.
Menu