gpt4 book ai didi

apache-zookeeper - 策展人 InterProcessMutex 与 InterProcessSemaphoreMutex

转载 作者:行者123 更新时间:2023-12-04 03:03:42 24 4
gpt4 key购买 nike

InterProcessMutexInterProcessSemaphoreMutex 有什么区别? docs假设 InterProcessSemaphoreMutexInterProcessMutex 相同,只是它不是可重入。但是我不知道可重入是什么意思。

最佳答案

我是 Apache Curator 的主要作者。无论文档可能会说什么或不会说什么,为了记录在案,我都想为这两个类中的每一个提供确切的用例。

InterProcessMutex

InterProcessMutex 应该在您需要能够以可重入方式锁定时使用。这意味着一个给定的线程一旦获得锁就被称为“拥有”,并且可以在需要时再次锁定它。如果线程将锁对象传递给其他不需要关心是否已获取锁的方法,这将很有用。请注意,这也意味着只有拥有线程才能释放锁。这是一个例子:

InterProcessMutex lock = new InterProcessMutex(...);

if ( !lock.acquire(...) ) ... // if acquire failed: throw, return, etc
try {
doWork(lock); // doWork() can safely call lock.acquire() again on the lock
} finally {
lock.release();
}

一旦获取,如果锁在与用于获取锁的线程不同的线程中释放,则抛出 IllegalMonitorStateException

InterProcessSemaphoreMutex

InterProcessSemaphoreMutex 是一种宽松版本的锁,不会记录获取它的线程。它具有更简单的语义。每个 InterProcessSemaphoreMutex 实例只能被获取一次,并且必须通过释放(在任何线程中)进行平衡。即

InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(...);

lock.acquire();
lock.acquire(); // this will block forever

我希望这会有所帮助。如果文档需要澄清,我们将不胜感激有改进的合并请求。

关于apache-zookeeper - 策展人 InterProcessMutex 与 InterProcessSemaphoreMutex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46653266/

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