PHP中文网2017-04-18 09:31:25
You can refer to my blog
The use of Synchronized for thread synchronization
Thread synchronization: Application of Synchronized in singleton mode
天蓬老师2017-04-18 09:31:25
This blog has the answer. However, the reference to global lock is easily misleading. Because sychoronized
是使用括号里的对象来上锁,而 String.class
is not all instances of the String class, but an object, an object of type Class. Therefore, if you use different such class objects to synchronize, you will obtain different locks.