写入小管道大小的对象时管道传输会阻塞
我当前正在测试的示例存在一些问题。由于某种原因,在 oos.writeObject(new SimpleObject()); 处写入时执行会阻塞,尽管管道应该传输数据,即使(我假设)如果它有由于管道尺寸较小,可以在较小的操作中完成此操作。无论如何,当管道大小大于对象时,该示例成功;当管道大小小于对象时,该示例失败。如果有人能对此有所了解,我们将不胜感激。
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.ByteBuffer;
import org.apache.mina.filter.codec.serialization.ObjectSerializationInputStream;
import org.apache.mina.filter.codec.serialization.ObjectSerializationOutputStream;
public class ObjTest4 {
public static void main(String[] args) {
System.out.println("exec1");
int objectsToSend = 10;
int objectsRecvd = 0;
try {
System.out.println("exec2");
PipedOutputStream pos = new PipedOutputStream();
ObjectSerializationOutputStream oos = new ObjectSerializationOutputStream(pos);
PipedInputStream pis = new PipedInputStream(pos, 500);
ObjectSerializationInputStream ois = new ObjectSerializationInputStream(pis);
oos.setMaxObjectSize(2000);
ois.setMaxObjectSize(2000);
while (objectsRecvd < objectsToSend) {
System.out.println("exec3");
oos.writeObject(new SimpleObject());
System.out.println("exec3.1");
oos.flush();
System.out.println("exec3.2");
System.out.println("oisavail: " + ois.available());
Object o = ois.readObject();
if (o != null) {
objectsRecvd++;
System.out.println("o: " + o);
} else {
System.out.println("recvd null");
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
以及正在序列化的对象的类:
package objtest;
import java.io.Serializable;
import java.util.EnumSet;
public class SimpleObject implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public String moo = "moo";
public EnumSet<EnumTest> set = EnumSet.of(EnumTest.Test);
public String moo2 = "moo2";
public String moo3 = "moo3";
public String moo4 = "moo4_";
{
for (int i = 0; i < 8; i++) {
moo4 += moo4;
}
}
/**
*
*/
public SimpleObject() {
// TODO Auto-generated constructor stub
}
/**
* @return the moo
*/
public String getMoo() {
return moo;
}
/**
* @param moo the moo to set
*/
public void setMoo(String moo) {
this.moo = moo;
}
}
干杯,
克里斯
I'm having a bit of problem with an example I'm currently testing. For some reason, the execution blocks when writing at oos.writeObject(new SimpleObject());
, despite that fact that the pipe should transfer the data across, even (I'd assume) if it had to do it in smaller operations due to a small pipe size. Anyway, the example succeeds when the pipe size is larger than the object, and fails when the pipe size is smaller than the object. If anyone could shed some light on this, it'd be much appreciated.
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.ByteBuffer;
import org.apache.mina.filter.codec.serialization.ObjectSerializationInputStream;
import org.apache.mina.filter.codec.serialization.ObjectSerializationOutputStream;
public class ObjTest4 {
public static void main(String[] args) {
System.out.println("exec1");
int objectsToSend = 10;
int objectsRecvd = 0;
try {
System.out.println("exec2");
PipedOutputStream pos = new PipedOutputStream();
ObjectSerializationOutputStream oos = new ObjectSerializationOutputStream(pos);
PipedInputStream pis = new PipedInputStream(pos, 500);
ObjectSerializationInputStream ois = new ObjectSerializationInputStream(pis);
oos.setMaxObjectSize(2000);
ois.setMaxObjectSize(2000);
while (objectsRecvd < objectsToSend) {
System.out.println("exec3");
oos.writeObject(new SimpleObject());
System.out.println("exec3.1");
oos.flush();
System.out.println("exec3.2");
System.out.println("oisavail: " + ois.available());
Object o = ois.readObject();
if (o != null) {
objectsRecvd++;
System.out.println("o: " + o);
} else {
System.out.println("recvd null");
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
and the class for the object being serialised:
package objtest;
import java.io.Serializable;
import java.util.EnumSet;
public class SimpleObject implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public String moo = "moo";
public EnumSet<EnumTest> set = EnumSet.of(EnumTest.Test);
public String moo2 = "moo2";
public String moo3 = "moo3";
public String moo4 = "moo4_";
{
for (int i = 0; i < 8; i++) {
moo4 += moo4;
}
}
/**
*
*/
public SimpleObject() {
// TODO Auto-generated constructor stub
}
/**
* @return the moo
*/
public String getMoo() {
return moo;
}
/**
* @param moo the moo to set
*/
public void setMoo(String moo) {
this.moo = moo;
}
}
Cheers,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,我发现了问题 - Java 文档说 不要使用来自单个线程的管道流,因为它可能会使线程死锁:
Sorry, I found the problem -- the Java documentation says not to use piped streams from a single thread, as it may deadlock the thread: