下面的一段程序:
public static void main(String[] args) {
    for(int i = 0;i < 10;i++) {
        System.out.println(i);
        System.err.println(k);
    }
}如上的程序输出为什么不是一黑一红呢,而是每次执行后输出的效果都不一样呢?

解决方案 »

  1.   

    /**
         * The "standard" error output stream. This stream is already
         * open and ready to accept output data.
         * <p>
         * Typically this stream corresponds to display output or another
         * output destination specified by the host environment or user. By
         * convention, this output stream is used to display error messages
         * or other information that should come to the immediate attention
         * of a user even if the principal output stream, the value of the
         * variable <code>out</code>, has been redirected to a file or other
         * destination that is typically not continuously monitored.
         */
        public final static PrintStream err = nullPrintStream();注意这句this output stream is used to display error messages
    or other information that should come to the immediate attention
    of a user even if.....out, has been redirected to 实际上无论你在err.print前加多少次打印的信息,程序总会让用户在第一时间看到err信息,毕竟,这是可能导致程序崩溃的信息,需要第一时间让用户知道
      

  2.   

    又转回来了明白了你的意思,也测试了一下,确实是随机出现。我的个人理解是:System.out、System.err都是已打开的输出流,通常只要调用print告诉它输出信息内容就可以了,而信息的具体显示过程是在独立的线内完成的。out.print()和err.print()是两个相互独立的,谁先谁后都是可能的;但是在一个线程内执行顺序绝对不会错。至于楼上给出的jdk说明,好像是说:err通常用途有两个(1)用来显示错误信息(2)用来显示那些系统认为需要引起用户注意的重要信息(在out被重定向到文件或其它看不到的地方)。个人判断 + 猜测,期待更多人讨论。
      

  3.   

    to:kevinliuu:
       如果是err.print是一个高优先级的操作的话,为什么出来的是随即的呢?
    to:pdvv(我爱猫猫):
       多谢关注,如你所说,期待更多人的讨论:)其实我也是无意当中看到这个东西,想不明白就上来问问,不知道这个问题在实际的应用中到底有什么作用,我也就不得而知了