Java – why do you sometimes print system. Java first Err statement?

In Java, I noticed that sometimes, system The err statement starts with the system Print before the out statement, although the latter appears first in my code Why? I'm curious.

Solution

Generally, system Out is a buffered output stream, so text is accumulated before it is flushed to the target location This can significantly improve the performance of applications that print large amounts of text because it minimizes the number of expensive system calls that must be made However, this means that the text is not always displayed immediately and may print out later than it was written

On the other hand, system Err is usually not buffered because error messages need to be printed immediately This is slow, but the intuition is that error messages may be time critical, so program deceleration may be reasonable According to the Javadoc for system err:

(my focus)

However, therefore, send to system Out's old data may be in a newer system Err appears after the message because the old buffered data is larger than the message sent to system Err refresh later For example, this event sequence:

>"Hello", buffered to system Out > send "panic" directly to system Err and print now. > "The world!" Buffer to system Out and print the buffered data

Will result in output

PANIC
Hello,world!

Even if you print to system.com from the panic Before err, hello is printed to system Out

I hope this can help!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>