title: NIO Thread CPU usage
body: <p>I am running into CPU usage problem when I am using a 
  <code>java.nio.channel.Selector</code>. 
  when the server thread started, it initially consume 200% cpu resource and dramatically drop down to 0.1%. but if it is connected by a client. this number rapidly increases to 97% - 100% and keep that number even after the client disconnected. </p>
  
  <p>here is the code I wrote for server.</p>
  
  <pre><code>package com.cs.gang.test;
  
  import java.io.File;
  import java.io.IOException;
  import java.io.RandomAccessFile;
  import java.net.InetSocketAddress;
  import java.nio.ByteBuffer;
  import java.nio.channels.SelectionKey;
  import java.nio.channels.Selector;
  import java.nio.channels.ServerSocketChannel;
  import java.nio.channels.SocketChannel;
  import java.util.Iterator;
  
  public final class TCPConnectionServer implements Runnable {
  
  private RandomAccessFile logFile;
  private Selector selector;
  
  public TCPConnectionServer() throws IOException {
      final File logFile = new File("server_log.txt");
      if (logFile.exists()) {
          logFile.delete();
      }
  
      this.logFile = new RandomAccessFile(logFile.getCanonicalPath(), "rw");
      System.out.println(logFile.getCanonicalPath());
  
      selector = Selector.open();
      final ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
      serverSocketChannel.configureBlocking(false);
      serverSocketChannel.socket().bind(new InetSocketAddress(8888));
      serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
  
  }
  
  @Override
  public void run() {
      while (true) {
          try {
              if (selector.select() &gt; 0) {
                  final Iterator&lt;SelectionKey&gt; keys = selector.selectedKeys().iterator();
                  while (keys.hasNext()) {
                      final SelectionKey key = keys.next();
                      keys.remove();
  
                      if (key.channel() instanceof SocketChannel) {
                          if (!((SocketChannel) key.channel()).isConnected()) {
                              logFile.writeChars(((SocketChannel) key.channel()).toString() + " is off line");
                          }
                      }
  
                      if (key.isAcceptable()) {
                          final ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
                          final SocketChannel clientChannel = serverSocketChannel.accept();
                          clientChannel.configureBlocking(false);
                          clientChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
                          logFile.writeChars(clientChannel.toString() + "is now connected");
                      } else if (key.isReadable()) {
  
                          final SocketChannel client = (SocketChannel) key.channel();
                          if (client.isConnected()) {
                              final ByteBuffer buffer = ByteBuffer.allocate(1024);
                              int byteRead = -1;
                              final StringBuilder sb = new StringBuilder(client.toString()).append(" : ");
  
                              while ((byteRead = client.read(buffer)) &gt; 0) {
                                  sb.append(new String(buffer.array()), 0, byteRead);
                                  buffer.clear();
                              }
                              logFile.writeChars(sb.toString());
                              System.out.println(sb.toString());
                          } else {
                              System.out.println("Closed Connection detected");
                          }
  
                      }
                  }
              } else {
                  System.out.println("Sleep for 100ms");
                  Thread.sleep(100);
              }
          } catch (final IOException e) {
              e.printStackTrace();
          } catch (final InterruptedException e) {
              e.printStackTrace();
          }
      }
  }
  
  public static void main(String[] args) throws IOException {
      new Thread(new TCPConnectionServer()).start();
  }
  }
  </code></pre>
  
  <p>can any one help me out? I am new to NIO and I am having absolutely no idea about this problem now.</p>
  
  <p>Thanks</p>