是的,如果方法不是static.
阿synchronized非静态方法同步上this.所以这个方法:
public synchronized void foo() {
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
实际上相当于这个:
public void foo() {
synchronized(this) {
// do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
甲static同步方法同步于当前类.所以像这样的方法:
public static synchronized void bar() {
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
实际上相当于这个:
public static void bar() {
synchronized(ThisClass.class) {
// do stuff
}
}
Run Code Online (Sandbox Code Playgroud)