This is probably an extremely basic question but I’m having difficulty finding an answer.
Is it ok to have one thread writing to a Socket’s output stream while another thread is reading from the Socket’s input stream?
Edit: This is a client application that is speaking to an external server. I am not trying to make the two threads talk to each other. Sorry for the ambiguity.
Assuming you mean the threads are using the same socket to communicate with an external actor and not with each other, this is pretty common as you often don’t want to block while waiting to read from the socket.
Thread1:
Thread2:
If instead you meant just having two threads communicate internally, you should look into non-socket alternatives.
Edit: Also beware of potential concurrency issues, so check whether the streams you are using are thread safe.