Name
Selector
Synopsis
A Selector
is an object that monitors multiple
nonblocking SelectableChannel
objects and (after
blocking if necessary) “selects”
the channel that is (or the channels that are) ready for I/O. Create
a new Selector
with the static open(
)
method. Next register the channels that it is to monitor:
a channel is registered by passing the Selector
to
the register( )
method of the channel
(register( )
is defined by the abstract
SelectableChannel
class). In addition to the
Selector
you must also pass a bitmask that
specifies which I/O operations (reading, writing, connecting, and
accepting) that the Selector
is to monitor for
that channel. Each call to this register( )
method
returns a SelectionKey
object. (The
SelectionKey
class also defines the constants that
are used to form the bitmask of I/O operations.) Note that before a
SelectableChannel
can be registered, it must be in
nonblocking mode, which can be accomplished with the
configureBlocking( )
method of
SelectableChannel
.
Once the channels are registered with the
Selector
, call select(
)
to block until one or more of the
channels is ready for I/O. One version of select(
)
takes a timeout value and returns if the specified number
of milliseconds elapses without any channels becoming ready for I/O.
These methods also return if any of the channels is closed, if an
error occurs on any channel, if the wakeup( )
method of the Selector
is called, or if the
interrupt( )
method of the blocked thread is called. There is ...
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.