This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes NIO question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "NIO question" Watch "NIO question" New topic

NIO question

Guiqiang Zhang

Joined: Jan 10, 2004
Posts: 6
I have write a new NIO server thread, which accept messages from client, but there is some problem. which make it loop too much quickly which make cpu usage to 100%, my run method is follow, any problem?

public void run() {
try {
// This map is used to store clientChannel - Connection pair set,
// store it while accepting a client socket, and retrieve it while reading client request.
final Map channelToConnection = new HashMap();

while (!shouldStop) {
// waiting for events with timeout value
if ( == 0) {

Iterator it = selector.selectedKeys().iterator();
// process every selectionKey
while (it.hasNext()) {
SelectionKey key = (SelectionKey);

// a client required a connection
if (key.isAcceptable()) {
synchronized (this) {
// reach the max connections limit
if ((this.maxConnections > 0)
&& (this.connectionIdToChannel.size() >= this.maxConnections)) {


// get client socket channel
SocketChannel client = server.accept();

// Set Non-Blocking mode for select operation

// recording to the selector (reading)
client.register(selector, SelectionKey.OP_READ);

Connection connection =
new Connection(generator.getNextUUID().toString(), this, client.socket());

synchronized (this) {
connectionIdToChannel.put(connection.getId(), client);

channelToConnection.put(client, connection);

// Notify all handlers to process this connection
for (Iterator connIter = this.handlers.values().iterator(); connIter.hasNext() {
} else if (key.isReadable()) {
// if isReadable = true then the server is ready to read

final SocketChannel client = (SocketChannel);

try {
Message request = null;
while ((request = IOHelper.readMessage(client, messageFactory)) != null) {
Handler handler = getHandler(request.getHandlerId());

// If no handler register for the given request, simply ignore the request.
if (handler != null) {
// Handler exists for the request.
handler.handleRequest((Connection) channelToConnection.get(client), request);
} catch (IOException e) {
// Failed to read from client, so close the corresponding connection
Connection con = (Connection) channelToConnection.remove(client);
} catch (ProcessingException e) {
} catch (IOException e) {
} finally {
synchronized (this) {
this.started = false;
this.shouldStop = false;

// close everything
try {
} catch (IOException e) {

try {
} catch (IOException e) {

this.selector = null;
this.server = null;

I help you , You help me!!
sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
Can you make your Thread sleep for a moment? For example 100 ms,

I think it will solve.

Add the following code in your loop ,before first "continue;" statement.


will solve i think.
It is sorta covered in the JavaRanch Style Guide.
subject: NIO question
Similar Threads
NIO Socket weirdnes in Solaris 2.10
does a nio socket buffer data internally too?
server can not accept all the clients connection request
nio and http request
ServerSocketChannel.accept() seems to create a random port to send data to?