解决方法
另一方面,System.err通常不会缓冲,因为错误消息需要立即打印.这是较慢的,但是直觉是错误消息可能是时间关键的,因此程序减速可能是合理的.根据the Javadoc for System.err
:
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 out,has been redirected to a file or other destination that is typically not continuously monitored.
(我的重点)
但是,因此,发送到System.out的旧数据可能会在较新的System.err消息之后出现,因为旧的缓冲数据比消息发送到System.err稍后刷新.例如这个事件序列:
>“Hello”,缓冲到System.out
>“PANIC”直接发送到System.err并立即打印.
>“世界!”缓冲到System.out,并打印缓冲的数据
会导致输出
PANIC Hello,world!
即使在PANIC打印到System.err之前,Hello被打印到System.out中.
希望这可以帮助!