【发布时间】: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