NIO Selector works in
In JDK1.4.2 added support for NIO to master one of the Selector think is the key to achieve good NIO.
Selector is used to obtain the registration of the channel in which the relevant events, that is, accept, read and write. selector has three key set.
key set: which includes representatives of all registered channel, you can selector.keys () are.
selected-key set: contains all be detected are concerned about the operation in place of the key, through selector.selectedKeys be.
cancelled-key set: contains all have been cancel, but no channel have not deregister the key, this set is not directly accessible.
key channel to register by calling the method is added to the key set in the. Canceled when the key in the select will be the key set from the corresponding removed.
key set itself can not be directly modified. Either by calling the close method or call channel key to cancel method, key will be placed in the canceled-key set. The key will cancel the channel will next select registration revoked, at the same time from all the key set key removed.
key in the implementation of the select operation is added to the selected-key set in the. In the selected-key set in the key can call the iterator's remove method, from the selected-key set is removed, can not approach it from the other selected-key set is removed.
The three methods by selector select (block select, until time of the incident out of concern obstruction), selectNow (non-blocking options), select (long) (specify time-out choices, out there are concerned about the arrival time of the incident or exit block ) to get concerned about the incident. The steps are divided into the following three steps:
1, will be present in the canceled-key set in the key from the key set of all removed, a revocation of the channel, clear the canceled-key set.
2, the operating system checks whether the formation events of concern, there is concern when the event occurs, first check the channel's key is already in the selected-key set, if not there, it is added to the selected-key set of to, and modify the key's ready-operation set ready to show the current operation, which previously existed in the ready-operation set of information will be deleted. If the corresponding key already exists in the selected-key set in, which directly modify its ready-operation set to indicate the current operation ready, remove the original ready-operation set of information.
3, if the second step in to canceled-key set in the key, the first step in this step will be the implementation of the operation.
selector itself is thread-safe, but his key set is not. Place in a selection process, the key changes concern the incident until the next select the time to take effect. In addition, key representatives of the channel and its may be cancel at any time and close. There is therefore the key in the key set does not mean that the key is valid, it does not mean the channel is open the. If the key may be canceled or closed the other thread channel, the synchronization program must carefully check these conditions.
The select can be blocked by calling the selector's wakeup method to wake up.