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.
You can refer to my blog
The use of Synchronized for thread synchronization
Thread synchronization: Application of Synchronized in singleton mode
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.