程序执行是非顺序的。为什么?

发布于 2024-10-16 02:16:34 字数 1704 浏览 4 评论 0原文

我正在考虑如何设置我的封装。

但我的程序正在以意想不到的顺序执行。这是我相当简单的代码:

“Main”:

package research.debug;

public class Main {

public static void main(String[] args) {

    Boolean b = Boolean.TRUE ;      

    Debug.black.printVariable( b, "b" ) ;
    Debug.red.printVariable( b, "b" ) ;

    System.out.println( "SUPPOSED to be inbetween..." ) ;

    Debug.black.println( "Hello" ) ;
    Debug.red.println( "Howdie" ) ;

}

}

“Debug”:

package research.debug;

public class Debug {

public static final Output black = new Output( Output.BLACK ) ;
public static final Output red = new Output( Output.RED ) ;

}

最后,“Output”:

package research.debug;

public class Output {

public static final int BLACK = 0 ;
public static final int RED = 1 ; 

private int outputMode = 0 ;

public Output( int outputMode ) {

    this.outputMode = outputMode ; 

}

public void println( String msg ) {

    if( outputMode == Output.BLACK ) {
        System.out.println( "Printed with black font: " + msg ) ;
    } else {
        System.err.println( "Printed with red font: " + msg ) ;
    }

}

public void printVariable( Object variable, String variableName ) {

    println( variableName + " = \"" + variable + "\"" ) ;

}

}

预期输出为:

用黑色字体打印:b = "true"

用红色字体打印:b = "true"

应该是在...

用黑色字体打印:Hello

用红色字体打印:Howdie

但是却超出了预期的顺序,就像这样:

用黑色字体打印:b =“true”

应该是中间...

用黑色字体打印:Hello

用红色字体打印:b =“true”

用红色字体打印:Howdie

发生了什么?

编辑:有时“应该在中间”消息会四处移动。无需我更改代码。

I was fooling around with how I could set up my encapsulation.

But my program is executing in an unexpected order. Here is my rather simple code:

The "Main":

package research.debug;

public class Main {

public static void main(String[] args) {

    Boolean b = Boolean.TRUE ;      

    Debug.black.printVariable( b, "b" ) ;
    Debug.red.printVariable( b, "b" ) ;

    System.out.println( "SUPPOSED to be inbetween..." ) ;

    Debug.black.println( "Hello" ) ;
    Debug.red.println( "Howdie" ) ;

}

}

"Debug":

package research.debug;

public class Debug {

public static final Output black = new Output( Output.BLACK ) ;
public static final Output red = new Output( Output.RED ) ;

}

And lastly, "Output":

package research.debug;

public class Output {

public static final int BLACK = 0 ;
public static final int RED = 1 ; 

private int outputMode = 0 ;

public Output( int outputMode ) {

    this.outputMode = outputMode ; 

}

public void println( String msg ) {

    if( outputMode == Output.BLACK ) {
        System.out.println( "Printed with black font: " + msg ) ;
    } else {
        System.err.println( "Printed with red font: " + msg ) ;
    }

}

public void printVariable( Object variable, String variableName ) {

    println( variableName + " = \"" + variable + "\"" ) ;

}

}

And the expected output would be:

Printed with black font: b = "true"

Printed with red font: b = "true"

SUPPOSED to be inbetween...

Printed with black font: Hello

Printed with red font: Howdie

But is instead out of the expected order, like this:

Printed with black font: b = "true"

SUPPOSED to be inbetween...

Printed with black font: Hello

Printed with red font: b = "true"

Printed with red font: Howdie

What's happening?

EDIT: Sometimes the "Supposed to be in between" message moves around. Without me changing the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

罗罗贝儿 2024-10-23 02:16:34

System.out 是缓冲的,而 System.err 不是,它们是两个不同的流,有些消息会发送到一个流,有些消息会发送到另一个流。

因此,这些混合消息可能不会按预期顺序出现,因为打印到 System.out 会被延迟,直到刷新缓冲区(手动或自动),而打印到 System.err code> 应该立即编写。

您可以通过调用flush()方法手动刷新流。

System.out is buffered and System.err is not, they are two different streams, and some of your messages go to one, some to the other.

Hence, these mixed messages may not appear in the expected order as the prints to System.out are delayed until the buffer is flushed (manually or automatically), whereas those to System.err should be written right away.

You can flush a stream manually by calling the flush() method.

十年不长 2024-10-23 02:16:34

您正在打印到 System.errSystem.out。尝试仅打印到 System.out 或使用 System.out.flush() 刷新缓冲区。

You are printing to System.err and System.out. Try to print only to System.out or use System.out.flush() to flush the buffers.

維他命╮ 2024-10-23 02:16:34

红/黑写入分别写入两个不同的流:System.errSystem.out

这些流是完全独立的并且在不同时间刷新。

唯一可以保证的事情(除非您使用多个线程)是您写入 System.out 的任何内容都将以与写入的顺序相同的顺序出现,对于 System.err 也是如此。 code>,但不保证它们如何混合在一起。

The red/black writes are writing into two different streams: System.err and System.out respectively.

These streams are completely independent and flush at different times.

The only thing that is guaranteed (unless you are using multiple threads) is that whatever you write to System.out will appear in the same order as written, and likewise for System.err, but no guarantees as to how they are mixed together.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文