Java Reference
In-Depth Information
size allocated will depend upon a number of factors related to the demands of the
particular application and the operating system for the particular platform, but, for
effi ciency's sake, should be on a kilobyte boundary. A 2 KB buffer allocation has
been chosen for the example and the pre-declaration of a
ByteBuffer
called
buffer
has been assumed.
buffer = ByteBuffer.allocate(2048);
There is also a method called
allocateDirect
that may be used to set up a buffer.
This attempts to allocate the required memory as direct memory, so that data does
not need to be copied to an intermediate buffer before being written to disc. This
means that there is the potential for I/O operations to be performed considerably
more quickly. Whether the use of direct buffers is appropriate or desirable (and
there will be a cost associated with the use of them, in terms of system resources)
depends upon the needs of the particular application and the characteristics of the
underlying operating system. In practice, multiple buffers and multiple threads (in
thread pools) will be needed for heavily used servers.
Once all of the above preparatory steps have been executed, the server will enter
a traditional
do…while(true)
loop that accepts connecting clients and pro-
cesses their data. The fi rst step within this loop is a call to method
select
on the
Selector
object. This returns the number of events of the type(s) that are being moni-
tored and have occurred. This method is very effi cient and appears to be based on
the Unix system call of the same name. Here's an example of its use, employing the
same
Selector
object called
selector
as was used previously in this section:
int numKeys = selector.select();
If no events have occurred since the last call of
select
, then execution loops back
to this call until there is at least one event detected.
For each event that is detected on a particular call to
select
, an object of class
SelectionKey
(package
java.nio.channels
) is generated and contains all the informa-
tion pertaining to the particular event. The set of
SelectionKey
s created by a given
call to
select
is called the
selected set
. The selected set is generated by a call to
method
selectedKeys
of the
Selector
object and is placed into a Java
Set
object. An
Iterator
object associated with the selected set is then created by a call to the
Set
object's
iterator
method. The lines to generate the selected set and its iterator are
shown below.
Set eventKeys = selector.selectedKeys();
Iterator keyCycler = eventKeys.iterator();
Using the above
Iterator
object, we can now work our way through the individ-
ual
SelectionKey
objects, making use of the
Iterator
methods
hasNext
and
next
. As
we retrieve each
SelectionKey
from the set, we need to typecast from type
Object
(which is how each key is held within the
Set
object) into type
SelectionKey
. Here is
the code required for detection and retrieval of each key:
while (keyCycler.hasNext())
Search WWH ::
Custom Search