gpt4 book ai didi

Java 围绕锁的同步语句

转载 作者:行者123 更新时间:2023-12-02 03:45:05 25 4
gpt4 key购买 nike

我想知道是否

synchronize (lock) {
...
}

其中lock是java.util.concurrent.locks.Lock的实例,将lock像任何其他对象或try-finally习惯用法一样对待,即

 lock.lock(); 
try {
...
} finally {
lock.unlock();
}

最佳答案

Lock documentation :

Note that Lock instances are just normal objects and can themselves be used as the target in a synchronized statement. Acquiring the monitor lock of a Lock instance has no specified relationship with invoking any of the lock() methods of that instance. It is recommended that to avoid confusion you never use Lock instances in this way, except within their own implementation.

所以基本上,它被视为任何其他对象。并且,不要这样做。

关于Java 围绕锁的同步语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1046193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com