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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章