简体   繁体   English

为什么在System.out.println之前打印System.err.println?

[英]Why is System.err.println is printed before then System.out.println?

I was tryingout Logger example from wiki chain of responsibility article. 我是从wiki责任链文章中试用Logger的例子。 running the example in idea it prints: 在它打印的想法中运行示例:

Sending to stderr: An error has occurred.
Writing to stdout: Entering function y.
Writing to stdout: Step1 completed.
Sending via email: Step1 completed.
Writing to stdout: An error has occurred.
Sending via email: An error has occurred.

but when i put a break point in stderrs writeMessage 但是当我在stderrs中写一个断点时写了一个

class StderrLogger extends Logger {
    public StderrLogger(int mask) {
        this.mask = mask;
    }

    protected void writeMessage(String msg) {
        System.err.println("Sending to stderr: " + msg);//break out here
    }
}

it prints all the messags except std err, there is no threads involved here, then why its printing stderr at first line in run case?. 它打印除了std err之外的所有messag,这里没有涉及的线程,那么为什么它在运行情况下的第一行打印stderr?

System.err flushes differently from System.out in Eclipse. System.err与Eclipse中的System.out不同地刷新。

Try this in Eclipse: 在Eclipse中试试这个:

public class Derp {
    public static void main(String[] args) {
        for(int i = 0; i < 10; i++) {
            System.out.println("OUT");
            System.err.println("ERR");
        }
    }
}

This will randomly print out most of the OUT s and most of the ERR s in big chunks. 这将以大块的形式随机打印出大部分OUT和大部分ERR However, this is an Eclipse issue, not a Java issue, as pointed out by Evgeniy Dorofeev. 然而,正如Evgeniy Dorofeev所指出的,这是一个Eclipse问题,而不是Java问题。

If you run this sample program in a terminal, you will notice the correct output, without any flushing needed. 如果在终端中运行此示例程序,您将注意到正确的输出,无需任何刷新。

Update: Thanks to Evgeniy Dorofeev for pointing this out! 更新:感谢Evgeniy Dorofeev指出这一点! The flushing not working is an Eclipse problem! 刷新不起作用是一个Eclipse问题!

System.out.println -> Sends the output to a standard output stream. System.out.println - >将输出发送到标准输出流。 Generally monitor. 一般监控。

System.err.println -> Sends the output to a standard error stream. System.err.println - >将输出发送到标准错误流。 Generally monitor. 一般监控。

The streams out and err are independent. outerr是独立的。

To get the desired output you must flush the streams or just use just one stream for all outputting. 要获得所需的输出,您必须刷新流或只使用一个流进行所有输出。

You can use System.out.flush(); 你可以使用System.out.flush(); and System.err.flush(); System.err.flush(); in your code 在你的代码中

如果您从Eclipse打印它,这是一个已知的错误https://bugs.eclipse.org/bugs/show_bug.cgi?id=32205

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM