private void rebuildSelector() {

        final Selector oldSelector = this.selector;
        final Selector newSelector;

        if (oldSelector == null) {
            return;
        }

        try {
            newSelector = openSelector();
        } catch (Exception e) {
            LOGGER.warn("Failed to create a new Selector.", e);
            return;
        }

        // 注册所有的channels到新的Selector.
        int nChannels = 0;
        for (; ; ) {
            try {
                for (SelectionKey key : oldSelector.keys()) {
                    Object a = key.attachment();
                    try {
                        if (key.channel().keyFor(newSelector) != null) {
                            continue;
                        }

                        int interestOps = key.interestOps();
                        key.cancel();
                        key.channel().register(newSelector, interestOps, a);
                        nChannels++;
                    } catch (Exception e) {
                        LOGGER.warn("Failed to re-register a Channel to the new Selector.", e);
                    }
                }
            } catch (ConcurrentModificationException e) {
                // Probably due to concurrent modification of the key set.
                continue;
            }

            break;
        }

        selector = newSelector;

        try {
            // time to close the old selector as everything else is registered to the new one
            oldSelector.close();
        } catch (Throwable t) {
            if (LOGGER.isWarnEnabled()) {
                LOGGER.warn("Failed to close the old Selector.", t);
            }
        }

        LOGGER.info("Migrated " + nChannels + " channel(s) to the new Selector.");
    }
private void rebuildSelector ( ) { final Selector oldSelector = this . selector ; final Selector newSelector ; if ( oldSelector == null ) { return ; } try { newSelector = openSelector ( ) ; } catch ( Exception e ) { LOGGER . warn ( "Failed to create a new Selector." , e ) ; return ; } int nChannels = 0 ; for ( ; ; ) { try { for ( SelectionKey key : oldSelector . keys ( ) ) { Object a = key . attachment ( ) ; try { if ( key . channel ( ) . keyFor ( newSelector ) != null ) { continue ; } int interestOps = key . interestOps ( ) ; key . cancel ( ) ; key . channel ( ) . register ( newSelector , interestOps , a ) ; nChannels ++ ; } catch ( Exception e ) { LOGGER . warn ( "Failed to re-register a Channel to the new Selector." , e ) ; } } } catch ( ConcurrentModificationException e ) { continue ; } break ; } selector = newSelector ; try { oldSelector . close ( ) ; } catch ( Throwable t ) { if ( LOGGER . isWarnEnabled ( ) ) { LOGGER . warn ( "Failed to close the old Selector." , t ) ; } } LOGGER . info ( "Migrated " + nChannels + " channel(s) to the new Selector." ) ; }
重新创建一个新的Selector, 来解决 java nio 在linux下 epoll CPU 100% 的bug
官方声称在JDK1.6版本的update18修复了该问题，但是直到JDK1.7版本该问题仍旧存在，只不过该bug发生概率降低了一些而已，它并没有被根本解决
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6403933
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=2147719