Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

OutputStream.write() show stopper - please help

843790Aug 2 2009 — edited Aug 4 2009
Hey all,

So I have this problem... Basically, I have a multi-threaded network application which has been deadlocking on me at completely arbitrary times. I have finally isolated the line of code that is causing the deadlock:

sock.getOutputStream().write();

Basically, anywhere that I try to write to a socket's output stream (I only write Strings in this application) it sometimes decides to just lock at that statement. So when a Thread synchronizes on a Collection of these sockets, and tries a write to one of them that doesn't properly error out, it just hangs (and holds the lock down) thus deadlocking all the other Threads that are waiting in line to use the Sockets Collection.

Now, I have a try/catch around the write statement, catching IOExceptions and SocketExceptions - basically, if the write fails, I just close and remove the socket. But it never gets that far. I mean, 99% of the time it works properly (if I close my socket on the client, the server properly attempts a write and it errors out and handles it accordingly). But some users seem to get their sockets in some weird state that, when written to, doesn't error out, or complete for that matter.

Any simple timeouts I can set to fix this?

I am prepared to do some serious modifying of my code base to create timers that spawn threads off for each Write command, so if it lasts more than 2 seconds to just kill the Thread (to let go of the lock) but that is a big pain in the *** and a lot of overhead if there is just some Timeout I can set that actually properly exits if the write() method doesn't return in time.

I know Socket.setSOTimeout(), but that only applies to read() calls, not write().

Thanks!
-Josh
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 1 2009
Added on Aug 2 2009
5 comments
861 views