A class lock
synchronized (String.class) {...}
An object lock
//doesn't matter what the lock object is as long as it's not null
private final Object lock = new Object();
...
synchronized (lock) {...} // will throw NPE if lock is null
They're both considered object
locks because String.class
returns an instance of Class<String>
.
In the class lock, the thread acquires the Class<String>
instance monitor. In the object lock, the thread acquires the String
instance monitor.