Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wrapping ObjectStream around pipes between threads

Status
Not open for further replies.

Icklepebble

Technical User
Jul 29, 2002
2
GB
Hi, I'm having a little trouble :)

I create two threads which are supposed to take objects from a stream, process them and return them to the main thread. So, I've created the threads, and connected them to the main thread using pipes, (one in and one out for each thread). Now I want to pass objects down these pipes, so I've got:

//Pipe created in main thread
PipedInputStream pipeIn = new PipedInputStream();

//Connect this pipe to the out pipe in thread1
thread1.out.connect(pipeIn);

//Now create object stream on this pipe
ObjectInputStream in = new ObjectInputStream(pipeIn);

The problem is that when I run the code, it hangs at the ObjectStream creation. No exceptions are thrown, and the system doesn't crash, it just appears to stop. i.e. If I put a print statement after, I never see it.

I hope somebody can see what I've done wrong... (or suggest a better way to pass objects between threads). Many thanks in advance for any response.

Matt
 
This is how I would do it.
Code:
PipedInputStream pin = new PipedInputStream();
PipedOutputStream pout = new PipedOutputStream(pin);

ObjectOutputStream oout = new ObjectOutputStream(pout);
ObjectInputStream oin = new ObjectInputStream(pin);
...
// in a thread body
// doing something here
oout.writeObject(obj);
// flush so that the inputstream sees the object
oout.flush();
...
// in a different thread
if oin.available() > 0 && some other stuff...
{
 obj = oin.readObject();
}

My guess is that you forgot to flush.
 
Cheers for that, I'll post an update to let u know how I get on..

MAtt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top