Re: Thread safe IO?
Thanks for the replies!
Esmond Pitt wrote:
You do realize that PrintWriter swallows all exceptions so you'll never
know about write failures?
Nope, didn't know this. I'll have to check this out more...
Bad idea. If any client isn't reading for any reason, writing to it will
eventually block, which will block this entire process. You need a
write thread per client. Or NIO.
I guess if one client is writing a lot, and not checking it's input,
then which ever server thread is writing to that client will eventually
block. Then all server threads will eventually block on that thread
which never relinquishes its lock.
It's not likely, given the nature of the app, but I see your point. I'm
probably going to be switching to passing (data) objects across a socket
soon, and the possibility of one object with a lot of data getting
stuffed in the socket is much higher. Data objects much larger than a
buffer or a window size are quite likely. NIO it is.
That part works OK. TCP gives you a full-duplex connection, and Socket
and its streams are thread-safe. You can even read and/or write with
multiple threads at the same time, but there are no guarantees about how
the data will be interleaved.
This was my real question. Is Java full duplex also for its Socket API?
The answer appears to be yes. So the API part is ok, but for design
issues I'll be using NIO or some form of non-blocking IO.
Thanks again! ^_^