Java:同步标准输出和标准错误 [英] Java: synchronizing standard out and standard error
问题描述
例如,我想写这个:
syso:aaa
syso:bbb
syso:ccc
syso:ddd
syso:eee
syserr:---
什么有时打印的是
aaa
bbb
ccc
---
ddd
eee
有时候两者之间没有新的一行, / p>
aaa
bbb
ccc ---
ddd
eee
每当我在输出上打印某些东西时,我用
System.out.flush();
或
System.err.flush();
如何解决这个问题?
Btw,一切都打印在Eclipse控制台中。
public class Util
synchronized public static void printToOut(...)
out.print(...)
synchronized public static void printToErr(...)
err.print(...)
I have a strange problem and it would be nice if I could solve it. For the debugging purposes (and some other things, as well) I'm writing a log of a console Java application on the standard output. Some things that are writen on standard out, and some things like errors are printed on standard error. The problem is that these two are not perfectly synchronized, so the order of printed lines is not always correct. I guess this is because many things are printed and it happens that a buffer for one output is full so the other output prints before the first one flushes it buffer.
E.g., I want to write this:
syso: aaa
syso: bbb
syso: ccc
syso: ddd
syso: eee
syserr: ---
What is sometimes printed is
aaa
bbb
ccc
---
ddd
eee
Sometimes there is not a new line in between, so it looks like
aaa
bbb
ccc---
ddd
eee
Every time I print something on an output, I flush the same output with
System.out.flush();
or
System.err.flush();
How to solve this problem? Btw, everything is printed in the Eclipse console.
public class Util
synchronized public static void printToOut(...)
out.print(...)
synchronized public static void printToErr(...)
err.print(...)
这篇关于Java:同步标准输出和标准错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!