- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
InterProcessMutex
和 InterProcessSemaphoreMutex
有什么区别? docs假设 InterProcessSemaphoreMutex
与 InterProcessMutex
相同,只是它不是可重入。但是我不知道可重入是什么意思。
最佳答案
我是 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/
InterProcessMutex 和 InterProcessSemaphoreMutex 有什么区别? docs假设 InterProcessSemaphoreMutex 与 InterProce
我是一名优秀的程序员,十分优秀!