Non-blocking UDP I/O vs blocking UDP I/O in Java(Java 中的非阻塞 UDP I/O 与阻塞 UDP I/O)
问题描述
Non-blocking TCP/IP SocketChannel
s and Selector
in NIO help me to handle many TCP/IP connections with small number of threads. But how about UDP DatagramChannels
? (I must admit that I'm not very familiar with UDP.)
UDP send operations don't seem to block even if the DatagramChannel
is not operating in blocking mode. Is there really a case where DatagramSocket.send(DatagramPacket)
blocks due to congestion or something similar? I'm really curious if there's such a case and what the possible cases exist in a production environment.
If DatagramSocket.send(DatagramPacket)
doesn't actually block and I am not going to use a connected DatagramSocket
and bind to only one port, is there no advantage of using non-blocking mode with DatagramChannel
and Selector
?
It's been a while since I've used Java's DatagramSockets, Channels and the like, but I can still give you some help.
The UDP protocol does not establish a connection like TCP does. Rather, it just sends the data and forgets about it. If it is important to make sure that the data actually gets there, that is the client's responsibility. Thus, even if you are in blocking mode, your send operation will only block for as long as it takes to flush the buffer out. Since UDP does not know anything about the network, it will write it out at the earliest opportunity without checking the network speed or if it actually gets to where it is supposed to be going. Thus, to you, it appears as if the channel is actually immediately ready for more sending.
这篇关于Java 中的非阻塞 UDP I/O 与阻塞 UDP I/O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 中的非阻塞 UDP I/O 与阻塞 UDP I/O


- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 获取数字的最后一位 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 转换 ldap 日期 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01