Is DatagramSocket thread safe?

Discussion in 'Java' started by ponce, Feb 26, 2006.

  1. ponce

    ponce Guest

    Hi all

    Does anyone know whether Java's DatagramSocket is thread safe?
    I am currently writing a multithreaded program that accesses the same
    DatagramSocket, and these threads are reading and writing simultanously
    from and to this same socket instance.

    Thanks!
    ponce, Feb 26, 2006
    #1
    1. Advertising

  2. ponce

    IchBin Guest

    ponce wrote:
    > Hi all
    >
    > Does anyone know whether Java's DatagramSocket is thread safe?
    > I am currently writing a multithreaded program that accesses the same
    > DatagramSocket, and these threads are reading and writing simultanously
    > from and to this same socket instance.
    >
    > Thanks!


    DatagramSocket is thread-safe.



    --

    Thanks in Advance...
    IchBin, Pocono Lake, Pa, USA
    http://weconsultants.servebeer.com/JHackerAppManager
    __________________________________________________________________________

    'If there is one, Knowledge is the "Fountain of Youth"'
    -William E. Taylor, Regular Guy (1952-)
    IchBin, Feb 26, 2006
    #2
    1. Advertising

  3. ponce

    Guest

    DataGramSocket is an abstraction of a type of socket. Thread-safety is
    dependent upon collisions of reads/writes resulting in an
    non-determinable state of your application.

    However the two together are not intrinsically a problem because of the
    way the socket is accessed -- .getInputStream() getOutputStream()

    writing to the outputstream has no thread implied impact on reading the
    inputstream from the Java viewpoint (the protocol you are working with
    might have something to say differently). More importantly would be how
    the OuputStream for the socket handles writing from multiple threads.

    As per Above, the socket is thread-safe

    Christian
    http://christian.bongiorno.org/
    , Feb 26, 2006
    #3
  4. ponce

    EJP Guest

    wrote:
    > DataGramSocket is an abstraction of a type of socket. Thread-safety is
    > dependent upon collisions of reads/writes resulting in an
    > non-determinable state of your application.
    >
    > However the two together are not intrinsically a problem because of the
    > way the socket is accessed -- .getInputStream() getOutputStream()


    Bzzt, DatagramSockets don't have streams. However:

    (i) DatagramSocket reads and writes are independent of each other.
    (ii) DatagramSocket writes are atomic so they are thread-safe.
    (iii) DatagramSocket reads are synchronized by Java and they are also
    atomic at the OS level so again they are thread-safe.
    EJP, Feb 27, 2006
    #4
  5. ponce

    ponce Guest

    Thank you so much for everyone's help!

    How do you all actually find all these information? I tried googling
    for it yesterday, but couldn't find any relevant info...

    EJP wrote:
    > wrote:
    >
    >> DataGramSocket is an abstraction of a type of socket. Thread-safety is
    >> dependent upon collisions of reads/writes resulting in an
    >> non-determinable state of your application.
    >>
    >> However the two together are not intrinsically a problem because of the
    >> way the socket is accessed -- .getInputStream() getOutputStream()

    >
    >
    > Bzzt, DatagramSockets don't have streams. However:
    >
    > (i) DatagramSocket reads and writes are independent of each other.
    > (ii) DatagramSocket writes are atomic so they are thread-safe.
    > (iii) DatagramSocket reads are synchronized by Java and they are also
    > atomic at the OS level so again they are thread-safe.
    ponce, Feb 27, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny Woods
    Replies:
    7
    Views:
    1,977
    Tim Ward
    Oct 14, 2003
  2. Josh
    Replies:
    0
    Views:
    888
  3. Replies:
    1
    Views:
    501
  4. Gabriel Rossetti
    Replies:
    0
    Views:
    1,300
    Gabriel Rossetti
    Aug 29, 2008
  5. John Nagle
    Replies:
    5
    Views:
    455
    John Nagle
    Mar 12, 2012
Loading...

Share This Page