System.out.println和System.err.println出现故障 [英] System.out.println and System.err.println out of order

查看:118
本文介绍了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天全站免登陆