why does ObjectInputStream constructor block reading a header

K

kartik

The ObjectInputStream constructor blocks till it reads a header from
the underlying input stream. This can cause deadlocks when you have a
pair of object streams communicating over sockets (it has happened to
me). Does anybody know why it may have been coded this way instead of
reading the header on the first call to readObject()?

-kartik
 
E

Esmond Pitt

kartik said:
The ObjectInputStream constructor blocks till it reads a header from
the underlying input stream. This can cause deadlocks when you have a
pair of object streams communicating over sockets (it has happened to
me). Does anybody know why it may have been coded this way instead of
reading the header on the first call to readObject()?

No but you can avoid the deadlock by creating the ObjectOutputStream
first and flushing it, then creating the ObjectInputStream.
 
T

Tony Morris

kartik said:
The ObjectInputStream constructor blocks till it reads a header from
the underlying input stream. This can cause deadlocks when you have a
pair of object streams communicating over sockets (it has happened to
me). Does anybody know why it may have been coded this way instead of
reading the header on the first call to readObject()?

-kartik

Yes - they have been written in accordance with specification and the
specification is the correct way of solving the problem.

The problem you have is attempting asynchronous I/O in the same thread.
This is just not possible.
 
K

kartik

Tony Morris said:
Because it includes non-blocking I/O.

But I do *not* want non-blocking I/O (and its associated complexity).
My only problem is that the ObjectInputStream constructor reads the
stream header before it really has to, and that's causing deadlock.
 
T

Tony Morris

But I do *not* want non-blocking I/O (and its associated complexity).
My only problem is that the ObjectInputStream constructor reads the
stream header before it really has to, and that's causing deadlock.

It doesn't cause deadlock.
Perhaps you meant a blocked I/O operation?
I'm not sure if that particular constructor does block, but it's no secret
that J2SE I/O has flaws, and that this is one of them (the inability to
cleanly break out of a blocked I/O state).
In most cases, even an interrupt fails.

There are texts on the topic.
I suggest "Taming Java Threads", Allen Holub.
There's another good one my Doug Lea (forget the title).
 
E

Esmond Pitt

Tony said:
It doesn't cause deadlock.

It does cause deadlock, between the peers, unless one of them constructs
the ObjectOutputStream first. Try it yourself.
Perhaps you meant a blocked I/O operation?

No he doesn't.
I'm not sure if that particular constructor does block

I am sure that that particular constructor does block until it receives
the stream header from the corresponding ObjectOutputStream()
constructor at the peer.
, but it's no secret
that J2SE I/O has flaws, and that this is one of them (the inability to
cleanly break out of a blocked I/O state).

'This' is not what the OP is talking about.
In most cases, even an interrupt fails.

There are texts on the topic.
I suggest "Taming Java Threads", Allen Holub.
There's another good one my Doug Lea (forget the title).

Tony, this is all irrelevant.
 
K

kartik

Tony Morris said:
It doesn't cause deadlock.
Perhaps you meant a blocked I/O operation?
I meant a deadlock. I have two machines that communicate using object
streams over sockets. Each tries to create the ObjectInputStream
before the ObjectOutputStream, but since the other side hasn't written
the stream header (because it hasn't constructed its output stream),
they deadlock.
I'm not sure if that particular constructor does block
It does. See http://java.sun.com/j2se/1.5.0/docs...m.html#ObjectInputStream(java.io.InputStream)
There are texts on the topic.
I suggest "Taming Java Threads", Allen Holub.
There's another good one my Doug Lea (forget the title).
Concurrent Programming in Java?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top