简体   繁体   中英

SocketChannel high CPU load on read

I have SocketChannel configured for read only SelectionKey.OP_CONNECT | SelectionKey.OP_READ SelectionKey.OP_CONNECT | SelectionKey.OP_READ Profiler shows runChannel is the most CPU consuming method and actually it is reasonable because it's infinite loop which calls method selector.select() all the time, but on the other hand I have dozens of such connections and it kills CPU.
Is there a possibility to decrease CPU load and in the same time do not miss any incoming message?

public void runChannel() {
    while (session.isConnectionAlive()) {
        try {
            // Wait for an event
            int num = selector.select();
            // If you don't have any activity, loop around and wait
            // again.
            if (num == 0) {
                continue;
            }
        } catch (IOException e) {
            log.error("Selector error: {}", e.toString());
            log.debug("Stacktrace: ", e);
            session.closeConnection();
            break;
        }
        handleSelectorkeys(selector.selectedKeys());
    }

}

从OP_CONNECT取消取消订阅-如果您已订阅OP_CONNECT并已连接,则select()不会阻止。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM