赞
踩
在做一个web日志浏览小工具时候 使用到了java的管道流. 但是使用过程中却一直报了 Write end dead 的异常.
我的需求是这样的:
这样在调试过程中一开始是没问题的,但第一拨日志输出到web完时总会报Write end dead 异常,经过排查 发现读取时候会去检测管道输出流的线程存活状态.
//管道输入流源码
synchronized void receive(int c) throws IOException {
if (!connected)
throw new IOException("Pipe not connected");
if (closedByWriter || closedByReader)
throw new IOException("Pipe closed");
if (readSide != null && !readSide.isAlive())
throw new IOException("Read end dead");
//将管道输出流当前的线程保存下来
writeSide = Thread.currentThread();
while (in == out) {
if (readSide != null && !readSide.isAlive())
throw new IOException("Pipe broken");
notifyAll();
try {
wait(1000L);
} catch (InterruptedException ex) {
throw new InterruptedIOException();
}
}
if (in < 0) {
in = 0;
out = 0;
}
buffer[in++] = (char) c;
if (in >= buffer.length)
in = 0;
}
public synchronized boolean ready() throws IOException {
if (!connected)
throw new IOException("Pipe not connected");
if (closedByReader)
throw new IOException("Pipe closed");
if (writeSide != null && !writeSide.isAlive() && !closedByWriter && in < 0)
throw new IOException("Write end dead");
return in >= 0;
}

可以看到报的异常和 管道是否关闭 reader是否关闭没关系(这两个报其他异常).这异常只和write流所在线程是否存活有关系
而且这 [所在线程] 并不是真正的所在线程,只是 他接收writte写入数据时候读取到的write的线程
log4j写日志时候并不是固定使用一个线程的,而且对于web应用产生日志的线程也绝对不会一样,这样就造成 其实管道输出流是被N+个线程轮流使用的,而且这些线程都在不断的创建和消亡,但管道输入流一直是固定一个线程在使用, 无解
这样使用管道流 无论如何都会报异常
我尝试 直接将两个管道流的源码copy下来自己创建了自己的管道流,并将 线程判定的那段代码注释掉 正常了!!!!
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.Reader;
public class MyPipeRead extends Reader {
public MyPipeRead(MyPipeWrite src) throws IOException {
this(src, 1024);
}
public MyPipeRead(MyPipeWrite src, int pipeSize) throws IOException {
closedByWriter = false;
closedByReader = false;
connected = false;
in = -1;
out = 0;
initPipe(pipeSize);
connect(src);
}
public MyPipeRead() {
closedByWriter = false;
closedByReader = false;
connected = false;
in = -1;
out = 0;
initPipe(1024);
}
public MyPipeRead(int pipeSize) {
closedByWriter = false;
closedByReader = false;
connected = false;
in = -1;
out = 0;
initPipe(pipeSize);
}
private void initPipe(int pipeSize) {
if (pipeSize <= 0) {
throw new IllegalArgumentException("Pipe size <= 0");
} else {
buffer = new char[pipeSize];
return;
}
}
public void connect(MyPipeWrite src) throws IOException {
src.connect(this);
}
synchronized void receive(int c) throws IOException {
if (!connected)
throw new IOException("Pipe not connected");
if (closedByWriter || closedByReader)
throw new IOException("Pipe closed");
if (readSide != null && !readSide.isAlive())
throw new IOException("Read end dead");
writeSide = Thread.currentThread();
while (in == out) {
if (readSide != null && !readSide.isAlive())
throw new IOException("Pipe broken");
notifyAll();
try {
wait(1000L);
} catch (InterruptedException ex) {
throw new InterruptedIOException();
}
}
if (in < 0) {
in = 0;
out = 0;
}
buffer[in++] = (char) c;
if (in >= buffer.length)
in = 0;
}
synchronized void receive(char c[], int off, int len) throws IOException {
while (--len >= 0)
receive(c[off++]);
}
synchronized void receivedLast() {
closedByWriter = true;
notifyAll();
}
public synchronized int read() throws IOException {
if (!connected)
throw new IOException("Pipe not connected");
if (closedByReader)
throw new IOException("Pipe closed");
// if (writeSide != null && !writeSide.isAlive() && !closedByWriter && in < 0)
// throw new IOException("Write end dead");
readSide = Thread.currentThread();
int trials = 2;
while (in < 0) {
if (closedByWriter)
return -1;
if (writeSide != null && !writeSide.isAlive() && --trials < 0)
throw new IOException("Pipe broken");
notifyAll();
try {
wait(1000L);
} catch (InterruptedException ex) {
throw new InterruptedIOException();
}
}
int ret = buffer[out++];
if (out >= buffer.length)
out = 0;
if (in == out)
in = -1;
return ret;
}
public synchronized int read(char cbuf[], int off, int len) throws IOException {
if (!connected)
throw new IOException("Pipe not connected");
if (closedByReader)
throw new IOException("Pipe closed");
// if (writeSide != null && !writeSide.isAlive() && !closedByWriter && in < 0)
// throw new IOException("Write end dead");
if (off < 0 || off > cbuf.length || len < 0 || off + len > cbuf.length || off + len < 0)
throw new IndexOutOfBoundsException();
if (len == 0)
return 0;
int c = read();
if (c < 0)
return -1;
cbuf[off] = (char) c;
int rlen = 1;
while (in >= 0 && --len > 0) {
cbuf[off + rlen] = buffer[out++];
rlen++;
if (out >= buffer.length)
out = 0;
if (in == out)
in = -1;
}
return rlen;
}
public synchronized boolean ready() throws IOException {
if (!connected)
throw new IOException("Pipe not connected");
if (closedByReader)
throw new IOException("Pipe closed");
// if (writeSide != null && !writeSide.isAlive() && !closedByWriter && in < 0)
// throw new IOException("Write end dead");
return in >= 0;
}
public void close() throws IOException {
in = -1;
closedByReader = true;
}
boolean closedByWriter;
boolean closedByReader;
boolean connected;
Thread readSide;
Thread writeSide;
private static final int DEFAULT_PIPE_SIZE = 1024;
char buffer[];
int in;
int out;
}

import java.io.IOException;
import java.io.Writer;
/**
* 描述
* @author Norton Lai
* @created 2018-8-26 上午10:45:00
*/
public class MyPipeWrite extends Writer {
public MyPipeWrite(MyPipeRead snk) throws IOException {
closed = false;
connect(snk);
}
public MyPipeWrite() {
closed = false;
}
public synchronized void connect(MyPipeRead snk) throws IOException {
if (snk == null)
throw new NullPointerException();
if (sink != null || snk.connected)
throw new IOException("Already connected");
if (snk.closedByReader || closed) {
throw new IOException("Pipe closed");
} else {
sink = snk;
snk.in = -1;
snk.out = 0;
snk.connected = true;
return;
}
}
public void write(int c) throws IOException {
if (sink == null) {
throw new IOException("Pipe not connected");
} else {
sink.receive(c);
return;
}
}
public void write(char cbuf[], int off, int len) throws IOException {
if (sink == null)
throw new IOException("Pipe not connected");
if ((off | len | off + len | cbuf.length - (off + len)) < 0) {
throw new IndexOutOfBoundsException();
} else {
sink.receive(cbuf, off, len);
return;
}
}
public synchronized void flush() throws IOException {
if (sink != null) {
if (sink.closedByReader || closed)
throw new IOException("Pipe closed");
synchronized (sink) {
sink.notifyAll();
}
}
}
public void close() throws IOException {
closed = true;
if (sink != null)
sink.receivedLast();
}
private MyPipeRead sink;
private boolean closed;
}

所以 在判定管道状态和流状态之后 还要判定线程状态的目的是什么 有什么作用?
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。