明确synchronized method 的 同步锁

class Test {
	int count;
        /*同步锁为 this */

	synchronized void bump() { count++; }

	static int classCount;

        /*同步锁为 当前class */ 

 	static synchronized void classBump() {
		classCount++;
	}
}


等同于

class BumpTest {
	int count;
	void bump() {
		synchronized (this) {
			count++;
		}
	}
	static int classCount;
	static void classBump() {
		try {
			synchronized (Class.forName("BumpTest")) {
				classCount++;
			}
		} catch (ClassNotFoundException e) {
				...
		}
	}
}

 

你可能感兴趣的:(明确synchronized method 的 同步锁)