Java.NIO 关于写混乱

Java.NIO on Write confusion

I have a confusion on the onWrite method of Java NIO, when the selector is ready to write should I:

1: Register/Change 一个 OP_Write

2:如果写returns0怎么办?

3:写完是注册还是改操作?

通道设置为非阻塞

....onAccept register OP_READ

public void onWrite()
{
            ByteBuffer bb = ByteBuffer.wrap(msg.encode());

            while(bb.remaining()!= 0){
                int ret = socketChannel.write(bb);

                if(ret == 0){

                    //Should I register or should I change op?

                    //socketChannel.register(selector, SelectionKey.OP_WRITE, null);

                    //selectionKey.interestOps(SelectionKey.OP_WRITE);
                }
            }

            //Is it necessary to change op or re-register Read or 0

            //socketChannel.register(selector, SelectionKey.OP_READ, null);
            //selectionKey.interestOps(SelectionKey.OP_READ);
            //selectionKey.interestOps(0);
}   

我正在努力实现尽可能低的延迟。通道不会回显,它只会读取或写入。

when the selector is ready to write should I:

1: Register/Change a OP_Write

已经注册了。这没有意义。

2: If Write returns 0 what should I do?

注册OP_WRITE。

3: After writing should I Register or Change ops?

如果 return 不为零,注销 OP_WRITE。

The channel is set to Non-Blocking

当然可以,不然什么都注册不了

基本上你把整个问题都从头到尾了。在情况 (2) 中,您应该 注册 OP_WRITE。不是所有的时间。有事就写,然后找案例2。然后,当OP_WRITE触发时,写并找案例3。