In Java NIO, why do we need to register interesting event types with Selector?

39 views Asked by At

Why does Java NIO register the event types of interest with the Selector instead of directly registering the SocketChannel/ServerSocketChannel of interest?

  1. In many NIO examples I see now, ServerSocketChannel is only interested in Accept events, and SocketChannel is only interested in read and write events. It seems that the code is always written this way, so why do we need to bring an event type parameter when registering SocketChannel with Selector?

  2. Are there any situations where SocketChannel is only interested in one type of read/write events, or neither?

  3. What will happen if keep sending data to a SocketChannel that has not registered the OP_READ event? The result of my test is that the cycle of sending data can go on forever. Is the data directly discarded at the sending end?

Where can I get tips on these 3 questions?

0

There are 0 answers