Writing to a socket: thread-safe?
I'm writing a simple multi-threaded networked Java application for practice. There is one thing that worries me: is writing to a Socket's output stream thread-safe? If I have multiple threads that write to a Socket's output stream (using PrintStreamWriter), would the messages overlap? If so, can someone offer a general suggestion to prevent that?
[358 byte] By [
v0dKAa] at [2007-10-3 11:29:20]

The messages probably won't overlap because of locking at the kernel level, but you'll still have a problem at the receiving end if you want to distinguish among the senders. It's usual to do this kind of multiplexing inside your application and have only one thread writing to the
ejpa at 2007-7-15 13:55:46 >

Though I am not entirely sure, I do not think that distinguishing among senders will be a problem. Though my application is multi-threaded, each thread belongs to a single entity: the server. Regardless of which thread sends the message, it is still a message from the server, and the client should heed it.
Or did you mean something else?
Would it be more sensical to create a separate thread for communication over the socket, and then have each thread talk to this communication thread instead of directly to the socket's output stream? Or is my current system acceptable, given that there is no need to distinguish among which thread sends which message?
v0dKAa at 2007-7-15 13:55:46 >
