System.out.println 和 System.err.println 乱序 [英] System.out.println and System.err.println out of order
问题描述
我的 System.out.println()
和 System.err.println()
调用没有按照我调用的顺序打印到控制台.
My System.out.println()
and System.err.println()
calls aren't being printed to the console in the order I make them.
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println("out");
System.err.println("err");
}
}
这会产生:
out
out
out
out
out
err
err
err
err
err
而不是交替out
和err
.这是为什么?
Instead of alternating out
and err
. Why is this?
推荐答案
它们是不同的流,在不同的时间刷新.
They are different streams and are flushed at different times.
如果你把
System.out.flush();
System.err.flush();
在您的循环中,它将按预期工作.
inside your loop, it will work as expected.
澄清一下,输出流被缓存,所以所有的写入都会进入这个内存缓冲区.安静了一段时间后,它们实际上被写出来了.
To clarify, output streams are cached so all the write goes into this memory buffer. After a period of quiet, they are actually written out.
您写入两个缓冲区,然后在一段时间不活动后,它们都会被刷新(一个接一个).
You write to two buffers, then after a period of inactivity they both are flushed (one after the other).
这篇关于System.out.println 和 System.err.println 乱序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!