程序执行是非顺序的。为什么?
我正在考虑如何设置我的封装。
但我的程序正在以意想不到的顺序执行。这是我相当简单的代码:
“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
System.out
是缓冲的,而System.err
不是,它们是两个不同的流,有些消息会发送到一个流,有些消息会发送到另一个流。因此,这些混合消息可能不会按预期顺序出现,因为打印到
System.out
会被延迟,直到刷新缓冲区(手动或自动),而打印到System.err
code> 应该立即编写。您可以通过调用
flush()
方法手动刷新流。System.out
is buffered andSystem.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 toSystem.err
should be written right away.You can flush a stream manually by calling the
flush()
method.您正在打印到
System.err
和System.out
。尝试仅打印到 System.out 或使用 System.out.flush() 刷新缓冲区。You are printing to
System.err
andSystem.out
. Try to print only toSystem.out
or useSystem.out.flush()
to flush the buffers.红/黑写入分别写入两个不同的流:
System.err
和System.out
。这些流是完全独立的并且在不同时间刷新。
唯一可以保证的事情(除非您使用多个线程)是您写入
System.out
的任何内容都将以与写入的顺序相同的顺序出现,对于System.err
也是如此。 code>,但不保证它们如何混合在一起。The red/black writes are writing into two different streams:
System.err
andSystem.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 forSystem.err
, but no guarantees as to how they are mixed together.