写入小管道大小的对象时管道传输会阻塞

发布于 2024-08-25 02:48:54 字数 2359 浏览 7 评论 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;
 }
}

干杯,
克里斯

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戒ㄋ 2024-09-01 02:48:54

抱歉,我发现了问题 - 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:

Attempting to use both objects from a
single thread is not recommended, as
it may deadlock the thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文