0

我目前正在测试的一个例子有点问题。出于某种原因,在写入时执行会阻塞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;
 }
}

干杯,
克里斯

4

1 回答 1

0

抱歉,我发现了问题——Java 文档说不要使用来自单个线程的管道流,因为它可能会使线程死锁

不建议尝试从单个线程中使用这两个对象,因为它可能会使线程死锁。

于 2010-03-12T16:59:55.113 回答