Java中C#lock的等价物?

use*_*311 8 c# java multithreading

Java中C#lock的等价物?

例如:

public int Next() {
  lock (this) {
    return NextUnsynchronized();
  }
}
Run Code Online (Sandbox Code Playgroud)

如何将此C#方法移植到Java?

ikh*_*ikh 18

public int Next() {
    synchronized (this) {
        return NextUnsynchronized();
    }
}
Run Code Online (Sandbox Code Playgroud)

而已.而下一个代码更好.

public synchronized int Next() {
    return NextUnsynchronized();
}
Run Code Online (Sandbox Code Playgroud)