This is the 28th day of my participation in the August Text Challenge.More challenges in August
Java NIO
The selector
Once the channel is in a ready state, it can transfer data between buffers. Non-blocking mode can be used to check whether the channel is ready, but the non-blocking mode will also do other tasks, when there are multiple channels at the same time, it is hard to check whether the channel is ready with other tasks separating them, or to do so is very complex, even if completed this functionality, but every check channel ready state, at least a system call, the price is very expensive. When you poll each channel for readiness, an unready channel that has just been checked is suddenly ready and is not detected until the next poll. The operating system has this ability to check readiness and notify readiness, so take advantage of what the operating system provides. In Java, the Selector class provides this abstraction, with the ability to ask if a channel is ready to perform each I/O operation, so a Selector is a good way to solve this problem.
The selector (Selector
)
The selector class manages information about this set of registered channels and their ready states. Channels are registered with a selector, which is used to update the ready state of the channel.
Alternative channels (SelectableChannel
)
This abstract class provides the public methods required for channel selectivity. FileChannel objects are not optional because they do not inherit SelectableChannel. All socket channels are optional, including those retrieved from the Pipe object. A SelectableChannel can be registered with a Selector object. A channel can be registered with multiple selectors, but only once for each Selector.
Select the key (SelectionKey
)
The selection key encapsulates the channel and selector registration relationship. Select key object is SelectableChannel. Register return and provide a tag says this registered relationship. Channels must be set to non-blocking mode before they can be registered with a selector (by calling configureBlocking(false))
Calling the register() method on the selectable channel registers it with a selector. If you try to register a channel in the blocking state, the register () will throw unchecked illegalBlockingModeException anomalies. In addition, once a channel is registered, it cannot go back to the blocked state. Will try to do so, in the call configureBlocking () method will throw illegalBlockingModeException anomalies. Also, an attempt to register a closed SelectableChannel instance will throw a losedChannelException.
The interest and ready collections of keys are channel specific. The implementation of each channel will be defined as its own select key class. It can be constructed in the register() method and passed to the supplied selector object.
If we were to write a server handler using non-blocking I/O, the steps would be as follows:
1. Register the event of interest with the Selector object
2. Get the event of interest from Selector
3, according to different events for the corresponding processing
In SelectionKey, four I/O operations are defined using static constants: IP_READ 1, OP_WRITE 4, OP_CONNECT 8, and OP_ACCEPT 16 are not the same in any 2, 3, or 4 values, so you can use the validOps() method to return the value to determine which operations SelectableChannel supports.
When the channel is closed, all relevant keys are automatically cancelled. When the selector is closed, all channels registered with the selector are deregistered, and the associated key is immediately invalidated (cancelled). Once the key is invalidated, the select-related method that calls it throws a CancelledKeyException.
The collection of keys registered with the keys() method may be empty. This collection of key registered is not can be directly modified, trying to do so will Java. Lang. UnsupportedOperationException.
The Selected key set is a subset of the set of registered keys, each member of which the associated channel is determined to be ready by the selector. This collection is returned (possibly empty) by the selected keys() method.
Cancelled key set is a subset of registered keys that contain keys for which the cancel() method was called (which has been invalidated), but which have not yet been Cancelled. This collection is a private member of the selector object and therefore cannot be accessed directly.
There are three ways to wake up a thread sleeping in the select() method:
A. Calling wakeup() on the Selector object causes the first unreturned selection on the Selector to be returned immediately. If no selection is currently in progress, the next call to one of the forms of the select() method will return immediately. Subsequent selection operations are normal. Calling the wakeup() method multiple times between select operations is no different from calling it once.
B. If the selector’s close() method is called, any thread that blocks during the selection operation will be woken up, just as the wakeup() method was called. But the channel associated with the selector is deregistered and the key is cancelled.
C. If the sleeping thread’s interrupt() method is called, its return status will be set. If the awakened thread later attempts to perform I/O operations on the channel, the channel is immediately closed and the thread catches an exception. The Selector object catches InterruptedException and calls the wakeup() method.