public void run() { try { while (selectable) { // The select() will be woke up if some new connection // have occurred, or if the selector has been explicitly // woke up if (selector.select() > 0) { Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator(); while (selectedKeys.hasNext()) { SelectionKey key = selectedKeys.next(); selectedKeys.remove(); if (key.isValid()) { EventHandler processor = ((EventHandler) key.attachment()); processor.process(key); } } } } } catch (IOException ioe) { LOGGER.log(Level.WARNING, "Error while waiting for events", ioe); } finally { if (selectable) { LOGGER.log( Level.WARNING, "Unexpected death of thread {0}", Thread.currentThread().getName()); } else { LOGGER.log( Level.FINE, "Thread {0} termination initiated by call to AgentServer.close()", Thread.currentThread().getName()); } } }