简体   繁体   English

为什么PrintStream.java中的println(Object x)方法从同步块外部调用String.valueOf()?

[英]Why does the println (Object x) method in PrintStream.java call String.valueOf() from outside the synchronized block?

Why does the println (Object x) method in PrintStream.java call String.valueOf() from outside the synchronized block? 为什么PrintStream.java中的println(Object x)方法从同步块外部调用String.valueOf()?

Why not use an existing print (Object obj)? 为什么不使用现有的打印(对象obj)?

for performance? 为了表现?

String.valueOf(x) calls the toString() method on x . String.valueOf(x)调用toString()上方法x The implementation of this method can do anything, including synchronization on this or other PrintStream instances. 此方法的实现可以执行任何操作,包括在此或其他PrintStream实例上进行同步。 To avoid deadlocks, the String.valueOf() needs to be called outside of a lock. 为了避免死锁,需要在锁之外调用String.valueOf()

The print method cannot be reused because the line break needs to be printed immediately after the object, even if there are other threads writing other data to the same PrintStream . 由于需要在对象之后立即打印换行符,因此即使有其他线程将其他数据写入同一PrintStream也无法重用print方法。 To achieve this, print and newLine are called from within a synchronized block. 为此,可在synchronized块内调用printnewLine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM