当 NIO 是单线程时,为什么 Netty 使用引用计数而不是每个连接只使用一个 ByteBuffer?

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

我很困惑为什么 Netty 5.0 让你对 ByteBuffer 使用引用计数。 Java NIO 不是应该是单线程的吗,换句话说,一个选择器线程用于多个连接?每个客户端都需要自己的 ByteBuffer,仅此而已,除非我遗漏了什么,否则不需要池化。

@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();
    }
}

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

这是因为我们不确切知道人们会用 ByteBuf 做什么。我们还支持从不同线程等写入