System.out.println 和 System.err.println 乱序 [英] System.out.println and System.err.println out of order

查看:57
本文介绍了System.out.println 和 System.err.println 乱序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 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

而不是交替outerr.这是为什么?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆