Why is my PipedOutputStream deadlock?… here is a solution to the problem.
Why is my PipedOutputStream deadlock?
I’m trying to implement a thread-loop buffer using PipedInputStream and PipedOutputStream, but it locks every time I go into mHead.write in Decoder runnable. I don’t think there will be deadlocks when using separate threads.
private class DecoderTask implements Runnable{
@Override
public void run() {
while(!mStop){
try {
Log.d(TAG,"trying to write");
mHead.write(decode( 0, 1000));
mHead.flush();
Log.d(TAG,"Decoded");
} catch (DecoderException e) {
Log.e(TAG,e.toString());
} catch (IOException e) {
Log.e(TAG,e.toString());
}
}
}
}
private class WriteTask implements Runnable{
@Override
public void run() {
while(!mStop){
try {
Log.d(TAG,"trying to read");
int read = mTail.read(mByteSlave, 0, mByteSlave.length);
mAudioTrack.flush();
mAudioTrack.write(mByteSlave,0,read);
Log.d(TAG,"read");
} catch (IOException e) {
Log.e(TAG,e.toString());
}
}
}
}
in some function
mTail = new PipedInputStream();
mHead = new PipedOutputStream(mTail);
mByteSlave = new byte[BUF];
mT1 = new Thread(new DecoderTask(), "Reader");
mT2 = new Thread(new WriteTask(), "Writer");
mT1.start();
mT2.start();
return;
EDIT: This is the full source of my service http://pastie.org/1179792
logcat prints out:
Try reading
Make an effort to write
Solution
I’ve had the same issue and solved the PIPE_SIZE by overriding the default In PipedInputStream( int) constructor. Method PipedOutputStream.write(byte[], int, int) blocks until all bytes are written to the output stream. This may be an issue with the default PIPE_SIZE.
After all, size matters 😉