Is DatagramSocket thread safe?

P

ponce

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!
 
I

IchBin

ponce said:
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-)
 
C

christian.bongiorno

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/
 
E

EJP

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.
 
P

ponce

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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top