File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes How can Socket.getInputStream() block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How can Socket.getInputStream() block " Watch "How can Socket.getInputStream() block " New topic

How can Socket.getInputStream() block

Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
Hi All,

I am trying to find an answer to the following weird condition.

I have written Dispatcher class that accepts client requests and forwards them to appropriate server instances. There are 3 server instances.

The code is some what as follows

However the program just hangs while trying to do s.getInputStream() above. The server is running and exeuctes fine.

Why does the program block at

[ April 22, 2005: Message edited by: Naveen Sampra ]
Joe Ess

Joined: Oct 29, 2001
Posts: 9190

The ObjectInputStream constructor blocks until it completes reading the serialization stream header. Code which waits for an ObjectInputStream to be constructed before creating the corresponding ObjectOutputStream for that stream will deadlock, since the ObjectInputStream constructor will block until a header is written to the stream, and the header will not be written to the stream until the ObjectOutputStream constructor executes. This problem can be resolved by creating the ObjectOutputStream before the ObjectInputStream, or otherwise removing the timing dependency between completion of ObjectInputStream construction and the creation of the ObjectOutputStream.

Java Object Serialization Specification

[How To Ask Questions On JavaRanch]
Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
Thanks for your input. I'm really perplexed with this problem.

In my code above as suggested in the quote, ObjectOutputStream is created "before" the ObjectInputStream. So why does ObjectInputStream still block ?
I agree. Here's the link:
subject: How can Socket.getInputStream() block
It's not a secret anymore!