简体   繁体   English

Java中C#lock的等价物?

[英]Equivalent of C# lock in Java?

Equivalent of C# lock in Java? Java中C#lock的等价物?

For example: 例如:

public int Next() {
  lock (this) {
    return NextUnsynchronized();
  }
}

How do I port this C# method to Java? 如何将此C#方法移植到Java?

public int Next() {
    synchronized (this) {
        return NextUnsynchronized();
    }
}

That's it. 而已。 And the next code is better. 而下一个代码更好。

public synchronized int Next() {
    return NextUnsynchronized();
}
java.util.concurrent.locks.Lock    
lock.lock();

    int newCount = ++count;
    lock.unlock();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM