Netty-(interestOps & readInterestOp) != 0写法的意义

//OP_READ = 1      00000001
//OP_WRITE = 4     00000100
//OP_CONNECT = 8   00001000
//OP_ACCEPT = 16   00010000
 protected final void removeReadOp() {
      SelectionKey key = selectionKey();
            // Check first if the key is still valid as it may be canceled as part of the deregistration
            // from the EventLoop
            // See https://github.com/netty/netty/issues/2104
            if (!key.isValid()) {
                return;
            }
            int interestOps = key.interestOps();
            if ((interestOps & readInterestOp) != 0) {
                // only remove readInterestOp if needed
                key.interestOps(interestOps & ~readInterestOp);
            }
        }


在NIO中,我们常见这种写法,那它到底是什么意思呢?
(interestOps & OP_READ ) != 0  如果不为0,说明interestOps一定包含OP_READ。
key.interestOps(interestOps & ~OP_READ);   ~OP_READ按位取反 11111110,那 interestOps&11111110,相当于把OP_READ去掉。

 

 

发布了175 篇原创文章 · 获赞 70 · 访问量 44万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章