So, I have two threads.
Thread one manages the client connections. (There is only one client and one server)
I call it my server thread.
Thread two manages sending messages to the client. I call it my message processor thread.
Thread one is responsible, among other things sending a heartbeat to the client periodically.
When programming I made an assumption that the sockets weren't thread safe, but the buffers were, and as long as I was using seperate buffers for the server and processor threads I would be fine.
I also made the assumption that the "PrintWriter" was analogous to the socket buffer in Java.
Under these assumptions I wrote this function to send a heartbeat:
public void sendHeartBeat(){
logger.info("Sending a hearbeat!");
PrintWriter printWriter=null;
try {
printWriter = new PrintWriter(clientSocket.getOutputStream());
} catch (IOException e) {
logger.info(e.toString());
}
if(printWriter!=null){
printWriter.print("HEARTBEAT#");
printWriter.flush();
}
}
The other thread, the "processor" one does something similar in that it does:
printWriter=new PrintWriter(theServer.getClientSocket().getOutputStream());
In this manner I would create a new "buffer" every time I wished to send a heartbeat, and my messages would never get overwritten.
Unfortuneately this does not seem to be the case. And I get an message coming through the pipe like this: dsgdsbHEARTBEAT#sdg
This causes a core dump later.
Here are my questions:
1) Sockets are obviously not thread safe, but are the PrintWriters I get from them thread safe? Or is it just returning the same PrintWriter?
2) What is analogous to the socket buffer in Java? How should I think about this problem?
3) How do I make it so that these threads do not write to the same buffer on the socket?
It's a poor design to have these multiple PrintWriter
s on the same stream. Really you want at least the object the calls them to be synchronised (or thread confined).
However, assuming for some reason you do want multiple PrintWriter
s:
First problem: Writer
s do not use this
as the lock. PrintWriter
and BufferedWriter
by default both use the Writer
they are constructed with as the lock. This is obviously completely broken. They should be using the Writer
's lock, not the Writer
itself. An easy mistake given that having locking a feature of Object
removes static type safety. So you'll need to construct a PrintWriter
with the socket OutputStream
(or some other common object) as the lock.
Secondly, we have buffering within PrintWriter
. So come the end of a buffer, half get written and half wait for the next write. To prevent that, either externally lock to combine a print
and flush
, or use auto-flushing and add a new line character.
So, it isn't meaningfully thread-safe, but you can hack it. Or you can use a better design.
You need a way to use the same PrintWriter
between the threads (t1.writer == t2.writer
, not just PrintWriter
s created from the same OutputStream
). With the same PrintWriter
, all writing operations are synchronized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With