简体   繁体   中英

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?

Why not use an existing print (Object obj)?

for performance?

String.valueOf(x) calls the toString() method on x . The implementation of this method can do anything, including synchronization on this or other PrintStream instances. To avoid deadlocks, the String.valueOf() needs to be called outside of a lock.

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 . To achieve this, print and newLine are called from within a synchronized block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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