简体   繁体   中英

Equivalent of C# lock in Java?

Equivalent of C# lock in Java?

For example:

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

How do I port this C# method to 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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