When reading the NIO source code, I saw the channel.register(selector, interestSet) method, which means what events are interested in when listening to this Channel through Selector
//可读
public static final int OP_READ = 1 << 0;
//可写
public static final int OP_WRITE = 1 << 2;
//可连接
public static final int OP_CONNECT = 1 << 3;
//可接受连接
public static final int OP_ACCEPT = 1 << 4;
Skill
When interested in readable events channel.register(selector, SelectionKey.OP_READ). What if you are interested in multiple events? ?
SelectionKey.OP_READ | SelectionKey.OP_WRITE Concatenate constants with bitwise OR operator,
//测试此通道是否可读
public final boolean isReadable() {
return (readyOps() & OP_READ) != 0;
}
//测试此通道是否可读
public final boolean isWritable() {
return (readyOps() & OP_WRITE) != 0;
}
//检查连接是否完成
public final boolean isConnectable() {
return (readyOps() & OP_CONNECT) != 0;
}
//测试这个SelectionKey对应的通道是否已经接受了一个新的Socket连接。
public final boolean isAcceptable() {
return (readyOps() & OP_ACCEPT) != 0;
}