Well, the DatagramSocket.receive(...) method is documented as a blocking operation. For example, if you have one thread that is trying to process packets from N different sockets, you will need to use NIO and selectors. Similarly, if the stream was to multiplex the check for new packets with other actions, you can do this.
If you do not have these or similar requirements, this will not help selectors. But this is no different from the case of TCP. You should not use selectors with TCP if you do not need them, because this potentially adds an additional system call.
(On Linux, in the case of the datagram, you do syscall select followed by recv ... instead of just recv .)
But if you are dealing with only one DatagramSocket, will the receiving method not read packets immediately as they arrive, regardless of whether they are from another computer?
If you are listening on one socket for datagrams from "all", then yes. If you have different sockets for different computers, then no.
And for a TCP comment, sometimes the use of a selector is justified simply by the fact that it is very resource intensive, requiring thousands of threads, as a blocking TCP server would require.
We did not discuss this case. But yes, thatโs true. And the same is true if you have thousands of thread locks on UDP.
My point was that you do not have many threads, or if it doesnโt matter , if the thread is blocked, then NIO does not help. In fact, this can reduce performance.
Stephen c
source share