Selector (selector)
Selector Workflow: We put the soketchannel we want to tell selector, we go to do other things, when something happens, selector will notify us, and then get Selectionkey, get the events we are interested in.
SELECOTR is a key class for Java NiO multiplexing, selector implements a thread to manage multiple channels, requiring fewer resources to handle more lanes, and saving overhead between threads, so Seletor is the time when the CPU was expensive, Many companies now have multi-core machines, making the best use of CPUs, and writing Java NIO's best practices Netty later.
Here are a few simple examples:
How Seletor is created:
Selector Selector = Selector.open ();
Channel Registration on Seletor:
Selectionkey key = Channel.register (Selector,selectionkey.op_read);
When the channel is registered to selector, a Selectionkey object is returned, Selectionkey represents the channel and the registered
Selector's relationship, Selectionkey maintains two important attributes Interestop s and Readyops.
Selector typically listens on four types of events:
-
Connect
-
Accept
-
Read
-
Write
These four kinds of events usually correspond to some of the Selectionkey:
-
Selectionkey.op_connect
-
Selectionkey.op_accept
-
Selectionkey. Op_read
-
Selectionkey. Op_write
Open the server-side socket channel Serversocketchannel Serversocketchannel = Serversocketchannel.open ();// The server side is set to non-blocking serversocketchannel.configureblocking (false);//The service side is binding serversocketchannel.bind (new Inetsocketaddress ( "localhost", 8000));//Register the event of interest Selector selector = Selector.open (); Selectionkey Selectionkey = Serversocketchannel.register (selector, selectionkey.op_accept); selectionkey.* You can view some of the events of interest
Select () Method:
int selectcount = Selector.select (), blocking to at least one re-registered channel ready for Selector.select (timeout); This has a time-out period Selector.sel Ectnow (); This is to go back in no matter what.
Selectedkeys () Method:
set<selectionkey> keys = Selector.selectedkeys (); Gets the iterator iterator<selectionkey> keyiterator = Keys.iterator ();
Get the events that we need to listen to;
while (Keyiterator.hasnext ()) { SelectionKey key = Keyiterator.next (); if (!key.isvalid ()) { continue; } if (Key.isacceptable ()) { ServerSocketChannel sscTemp = ( Serversocketchannel) key.channel (); // Get a connected socketchannel and register it on the selector, with an interest operation of Read socketchannel socketchannel = ssctemp.accept (); Socketchannel.configureblocking (false); socketchannel.register ( Selector, selectionkey.op_read); system.out.println (" Register channel , channel number is: " + Selector.keys (). Size ()); } else if (Key.isreadable ()) { //reading the data in the channel socketchannel channel = (Socketchannel) key.channel (); Read (channel); } keyiterator.remove (); //the event has been processed and can be discarded}
JAVA NIO Selector Knowledge III