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) {
...
}
}
}