The root cause is here:
while (dis.available()>0){
What happens is the consumer thread is able to consume all of the data sometimes. There's nothing available so it breaks early.
The exception gets thrown because PipedInputStream/PipedOutputStream keep track of what threads are reading and writing. There is a private method called checkStateForReceive
that throws, basically it is complaining that your consumer thread has ended:
} else if (readSide != null && !readSide.isAlive()) {
throw new IOException("Read end dead");
}
(readSide
is the consumer Thread.)
You can see this get called in the stack trace:
java.io.IOException: Read end dead at java.io.PipedInputStream.checkStateForReceive(PipedInputStream.java:246) at java.io.PipedInputStream.receive(PipedInputStream.java:210) at java.io.PipedOutputStream.write(PipedOutputStream.java:132) at java.io.DataOutputStream.writeLong(DataOutputStream.java:207) at java.io.DataOutputStream.writeDouble(DataOutputStream.java:242)
So write
calls receieve
on the input stream, which calls checkStateForReceieve
and throws if the reading Thread isn't alive.
I think your loop condition should just be:
while(!isDone.get()) {
Unrelated to the exception, I also think you might have essentially an infinite loop here:
if (avg>1E5)
isDone.set(true);
Your number generator generates numbers between 100 and 101 so averaging them will never be greater than 1e5. You might have meant to check if runningTotal > 1E5
.
Also:
- Don't forget to close your streams when you are done with them.
- Your consumer thread catches IOException outside its loop but the producer catches inside the loop. The producer doesn't abort if there's an exception: it just keeps going, catching exceptions over and over. You might want to move the catch to outside the loop like the consumer.
About your edit:
If you want to close the streams in main, you can join
on the threads.
try(
PipedOutputStream pos= new PipedOutputStream();
PipedInputStream pis =new PipedInputStream(pos)
){
AtomicBoolean isDone = new AtomicBoolean(false);
NumGen ng = new NumGen(pos,isDone);
RunningAvg ra = new RunningAvg(pis,isDone);
ng.start();
ra.start();
try {
ng.join(); // wait for ng and ra to complete
ra.join(); //
} catch(InterruptedException ie) {}
} catch (IOException e) {
e.printStackTrace();
}