简体   繁体   中英

Why Netty uses reference counting instead of just one ByteBuffer per connection when NIO is single-threaded?

I am confused why Netty 5.0 makes you use reference counting for ByteBuffers. Isn't Java NIO supposed to be single-threaded, in other words, one selector thread for many connections? Each client needs its own ByteBuffer and that's it, no pooling should be needed unless I am missing something.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    ByteBuf m = (ByteBuf) msg; // (1)
    try {
        long currentTimeMillis = (m.readUnsignedInt() - 2208988800L) * 1000L;
        System.out.println(new Date(currentTimeMillis));
        ctx.close();
    } finally {
        m.release();
    }
}

Source: http://netty.io/wiki/user-guide-for-5.x.html

It's because we don't know exactly what people will do with the ByteBuf. Also we support writing from different threads etc.

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