可能重複:
What is the difference between synchronized and static synchronized?有線程對象鎖VS級鎖
什麼是有過類鎖定對象鎖的優勢在哪裏?
例如,
public static void log2(String msg1, String msg2){
synchronized(MyClass.class){
log.writeln(msg1);
log.writeln(msg2);
}
}
,並
public void log2(String msg1, String msg2){
synchronized(this){
log.writeln(msg1);
log.writeln(msg2);
}
}
僅在靜態方法中需要鎖定類。 –