我正在尝试使用管道输入流来编写数据。但是从线程转储看起来管道输入流上有锁。
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos);
FileInputStream fis = null;
GZIPOutputStream gos = null;
byte[] buffer = new byte[1024];
try {
fis = new FileInputStream(file);
gos = new GZIPOutputStream(pos);
int length;
while ((length = fis.read(buffer, 0, 1024)) != -1)
gos.write(buffer, 0, length);
} catch(Exception e){
print("Could not read the file");
}
finally {
try {
fis.close();
gos.close();
}catch (Exception ie){
printException(ie);
}
}
writeObject(pis);
pos.close();
writeobj方法将简单地从流中读取,但是read方法被锁定。 线程转储表明管道输入流有一些等待。
main" prio=10 tid=0x08066000 nid=0x48d2 in Object.wait() [0xb7fd2000..0xb7fd31e8]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0xa5c28be8> (a java.io.PipedInputStream)
at java.io.PipedInputStream.awaitSpace(PipedInputStream.java:257)
at java.io.PipedInputStream.receive(PipedInputStream.java:215)
- locked <0xa5c28be8> (a java.io.PipedInputStream)
at java.io.PipedOutputStream.write(PipedOutputStream.java:132)
at java.util.zip.GZIPOutputStream.finish(GZIPOutputStream.java:95)
at java.util.zip.DeflaterOutputStream.close(DeflaterOutputStream.java:146)
Locked ownable synchronizers:
- None
我不确定是谁锁定了它。阅读文档以找出锁定调用。但无法弄清楚出了什么问题以及如何克服它。
答案 0 :(得分:5)
使用PipedInputStream和PipedOutputStream必须位于不同的线程中。
仔细阅读Javadoc: http://docs.oracle.com/javase/6/docs/api/java/io/PipedInputStream.html
通常,一个线程从PipedInputStream对象读取数据,而其他线程将数据写入相应的PipedOutputStream。建议不要尝试使用单个线程中的两个对象,因为它可能使线程死锁。
答案 1 :(得分:4)
PipedInputStream有一个小的非扩展缓冲区。缓冲区满后,写入PipedOutputStream块,直到缓冲输入由另一个线程读取。你不能在同一个线程中使用这两个,因为写操作将等待不可能发生的读操作。
在您的情况下,在您编写所有数据之前,您不会阅读任何数据,因此解决方案是使用ByteArrayOutputStream和ByteArrayInputStream代替:
答案 2 :(得分:0)
我需要一个过滤器来拦截慢速连接,我需要尽快关闭数据库连接,所以我最初使用Java管道但是当仔细观察它们的实现时,它全部同步,所以我最终使用一个小缓冲区创建了我自己的QueueInputStream阻塞队列将缓冲区放入队列一旦已满,它是无锁的,除非对于LinkedBlockingQueue使用的锁定条件,借助于小缓冲区它应该是便宜的,这个类只用于单个每个实例的生产者和消费者:
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.*;
public class QueueOutputStream extends OutputStream
{
private static final int DEFAULT_BUFFER_SIZE=1024;
private static final byte[] END_SIGNAL=new byte[]{};
private final BlockingQueue<byte[]> queue=new LinkedBlockingDeque<>();
private final byte[] buffer;
private boolean closed=false;
private int count=0;
public QueueOutputStream()
{
this(DEFAULT_BUFFER_SIZE);
}
public QueueOutputStream(final int bufferSize)
{
if(bufferSize<=0){
throw new IllegalArgumentException("Buffer size <= 0");
}
this.buffer=new byte[bufferSize];
}
private synchronized void flushBuffer()
{
if(count>0){
final byte[] copy=new byte[count];
System.arraycopy(buffer,0,copy,0,count);
queue.offer(copy);
count=0;
}
}
@Override
public synchronized void write(final int b) throws IOException
{
if(closed){
throw new IllegalStateException("Stream is closed");
}
if(count>=buffer.length){
flushBuffer();
}
buffer[count++]=(byte)b;
}
@Override
public synchronized void write(final byte[] b, final int off, final int len) throws IOException
{
super.write(b,off,len);
}
@Override
public synchronized void close() throws IOException
{
flushBuffer();
queue.offer(END_SIGNAL);
closed=true;
}
public Future<Void> asyncSendToOutputStream(final ExecutorService executor, final OutputStream outputStream)
{
return executor.submit(
new Callable<Void>()
{
@Override
public Void call() throws Exception
{
try{
byte[] buffer=queue.take();
while(buffer!=END_SIGNAL){
outputStream.write(buffer);
buffer=queue.take();
}
outputStream.flush();
} catch(Exception e){
close();
throw e;
} finally{
outputStream.close();
}
return null;
}
}
);
}