Java MulticastSocket线程安全吗? [英] Is Java MulticastSocket threadsafe?

查看:120
本文介绍了Java MulticastSocket线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个线程. 第一个发送带有MulticastSocket循环的数据报;第二个发送数据报.第二个线程在循环中使用相同的MulticastSocket实例接收数据报.

I have two threads. First one sends datagrams with a MulticastSocket in loop; the second thread receives datagrams using the same instance of MulticastSocket in loop.

它似乎工作正常,但我仍然对此表示怀疑.

It seems to be working properly, but I'm still in doubts.

这两个线程可以使用MulticastSocket的相同实例吗?就发送/接收方法调用而言,MulticastSocket线程安全吗?

Can these two threads use the same instance of MulticastSocket? Is MulticastSocket threadsafe in respect send/receive methods invocation?

推荐答案

发送和接收DatagramSocket方法在发送/接收数据报包上都是同步的.换句话说,如果您使用相同的数据报包从两个不同的线程发送和接收,则这两种方法将被同步,因为它们将使用同一对象作为同步令牌.

Both send and receive DatagramSocket methods are synchronized on the sending/receiving datagram packet. In other words if you are using a same datagram packet to send and receive from two different threads these two methods will be synchronized as they are going to use the same object as a synchronization token.

只要查看 DatagramSocket .

这篇关于Java MulticastSocket线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆