I have comeup with a multithreaded TCP server to handle reads and writes. My main objective is to separate out the reading and writing by two different threads. Hence I have a separate thread pool for writing to socket's outputstream. As soon as the read is finished by a thread and inserted into a queue (ArrayBlockingQueue), another thread from a pool will pick this up from the queue and write into the outputstream. Now the question is, I wanted to know when multiple threads try to write to the SAME socket's output stream; - how will be the behaviour - is this managed internally or needs to be handled specially - does the bytes gets messed (mixed) up
What would be a better approach in this case?
EDIT: My Question is not the same as, Are parallel calls to send/recv on the same socket valid?. The link is talking about POSIX and I forgot to mention that my question is specific about JAVA socket (socketWrite). To answer EJP's comment, yes, ours is a TCP protocol that has the use case of writing to the same output stream by multiple threads (to improve performance). Currently to solve my problem, I have synchronized outputstream;
synchronized (outputStream) {
outputStream.write(responseMessage);
outputStream.flush();
}