【问题标题】:Java Piped Input/Output Stream massive delay in communicationJava Piped Input/Output Stream 通信中的大量延迟
【发布时间】:2017-05-20 18:16:38
【问题描述】:

我正在制作一个使用管道输入/输出流的 java 应用程序(在它们周围有一个对象输入/输出流包装器)。发送数据时我遇到了很大的延迟,最初我认为这是因为我的数据大小。但是,以下演示显示了在不同线程上通信时管道输入/输出流中的本机延迟。

public class Main{
public static long millis = 0;

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    JButton button = new JButton("Press me");

    frame.getContentPane().add(button);
    frame.setSize(new Dimension(500, 500));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    PipedInputStream pis = new PipedInputStream();
    PipedOutputStream pos = new PipedOutputStream(pis);

    button.addActionListener(e -> {
        try {
            pos.write((int) (Math.random() * 1000));
            //records time the packet was sent
            millis = System.currentTimeMillis();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    });

    while (true) {
        System.out
                .println("recieved: " + pis.read() + "\n\tin time (ms): " + (System.currentTimeMillis() - millis));
    }

}

}

对我来说,这个延迟通常在 300-900 毫秒左右。这在我的应用程序中相当大。我想知道为什么延迟这么大?以及如何修复我的代码以减少延迟?

提前致谢。

【问题讨论】:

  • 冲洗怎么样?
  • 哦,哇,这是我的一个愚蠢的错误,谢谢你哈哈哈。
  • 现在可以使用了,谢谢。

标签: java multithreading inputstream outputstream


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
  • 2021-02-12
  • 2016-03-16
相关资源
最近更新 更多