[英]Is Java DatagramSocket class thread-safe?
If multiple threads write to the same socket, will be the data possibly corrupted?如果多个线程写入同一个套接字,数据是否可能损坏? (eg, a packet sent to incorrect recipient).
(例如,发送给错误接收者的数据包)。
DatagramSocket datagramSocket = new DatagramSocket(PORT);
new Thread(() -> {
while (true) datagramSocket.send(<DATAGRAM_PACKET_RECIPIENT_A>);
}).start();
while (true) datagramSocket.send(<DATAGRAM_PACKET_RECIPIENT_B>);
DatagramSocket#send
is synchronized on the data packet you send, so yes. DatagramSocket#send
在您发送的数据包上同步,所以是的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.