• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NIO question

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 (selector.select() == 0) {
continue;
}

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

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

it.remove();

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

// Set Non-Blocking mode for select operation
client.configureBlocking(false);

// 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() {
((Handler) connIter.next()).onConnect(connection);
}
} else if (key.isReadable()) {
// if isReadable = true then the server is ready to read
it.remove();

final SocketChannel client = (SocketChannel) key.channel();

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) {
this.setLastException(e);
// Failed to read from client, so close the corresponding connection
Connection con = (Connection) channelToConnection.remove(client);
this.closeConnection(con.getId());
}
}
}
}
} catch (ProcessingException e) {
this.setLastException(e);
} catch (IOException e) {
this.setLastException(e);
} finally {
synchronized (this) {
this.started = false;
this.shouldStop = false;

// close everything
try {
this.selector.close();
} catch (IOException e) {
e.printStackTrace();
}

try {
this.server.close();
} catch (IOException e) {
e.printStackTrace();
}

this.selector = null;
this.server = null;
this.connectionIdToChannel.clear();
}
}
}
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

this.sleep(100);

will solve i think.
reply
    Bookmark Topic Watch Topic
  • New Topic