我想实现一个带PipedInputStream的线程化循环缓冲区& PipedOutputStream,但是当我到达解码器可运行的mHead.write时,它每次都会锁定。我认为使用单独的线程时没有发生死锁的机会。为什么我的PipedOutputStream死锁?
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;
编辑:这里是我的服务的完整源http://pastie.org/1179792
logcat的打印输出:
试图读取
试图写
还没工作,我上面的编辑我的代码,以反映我现在,日志输出说道:试图写试图读取,然后什么都没有,其阻塞读和写。 – schwiz 2010-09-24 19:28:57
问题是你指出,以及写(字节[])没有定义PipedOutputStream所以它不工作,你必须使用(字节[],int,int)一个 – schwiz 2010-09-24 20:47:12
这不工作像我认为它确实如此,一旦我从mTail读取数据后,如何清除数据,它会一遍又一遍读取相同的数据。 – schwiz 2010-09-24 21:17:46