Java NIO Selector
Selector is a mutiplexer.
Basically aggregate multiple channel into one bundle so we don’t have to switch between different thread. By doing so, we can use one thread to handle multiple channel.
Seems like there is only one way to create selector.
Each channel must register to selector is order to subscribe to coming event.
The second parameter is
interest operations, which is a list of operations that this channel is interested in.
As a client, waiting for server to accept my connection request
As a server, waiting for client connection request
Waiting for data reading
Waiting for data writing
interest operations are just integer number, so they can be combined using bitwise operator like
A|B, to indicate multiple interests for single channel.
Now that we have channel registered, let us waiting for incoming request.
Then we need to know which exact channel get ready.
When want to close channel connection, we may use
This will deregister itself from channel as well.
Shutdown entire selector by closing it.