Search code examples
javasendmulticastsocket

Is MulticastSocket.send() reentrant?


I have 2 threads that want to invoke:

multicastSocket.send(dP1) and

multicastSocket.send(dP2)

respectively, where dP1 and dP2 are different DatagramPacket objects, and multicastSocket is the shared instance of MulticastSocket.

I don't understand if concurrency problems may happen on multicastSocket, if the threads call send() in the same moment.


Solution

    • DatagramSocket reads and writes are independent of each other.
    • DatagramSocket writes are atomic so they are thread-safe.
    • DatagramSocket reads are synchronized by Java and they are also atomic at the OS level so again they are thread-safe.
    • MulticastSocket inherits these methods from DatagramSocket so the same applies to it.