Why do we write Synchronized(ClassName.class)
问题 I have a question in singleton pattern. In singleton pattern we write synchronized(ClassName.class){ // other code goes here } What is the purpose of writing ClassName.class ? 回答1: In a member method (non-static) you have two choices of which monitor (lock) to use: " this " and " my class's single static lock ". If your purpose is to coordinate a lock on the object instance, use " this ": ... synchronized (this) { // do critical code } or public synchronized void doSomething() { ... } However