如何在Java中实现二进制信号量类?
我可以看到如何在 Java 中实现“标准”信号量类。但是,我不知道如何在 Java 中实现二进制信号量类。这样的实施如何运作?我应该什么时候调用唤醒和通知方法来唤醒和停止信号量上的线程? 我了解二进制信号量是什么,但我不知道如何对它们进行编码。
编辑注意:意识到我说的是“BINARY”信号量类。我已经完成了标准信号量类,并且我知道它是正确的,因此标准信号量类对我不感兴趣。
I can see how a "standard" Semaphore Class can be implemented in Java. However, I cant see how to implement a Binary Semaphore Class in Java. How does such implementation work? When should I call the wake and notify methods to wake and stop the threads that are on the semaphores?
I understand what binary semaphores are, but I have no idea of how to code them.
Edit Note: Realize that I said "BINARY" Semaphore class. The standard Semaphore class I already did and I know its correct so the standard Semaphore Class does not interest me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为您正在谈论互斥锁(或互斥锁)。如果是这样,您可以使用内在锁。 Java 中的这种锁充当互斥体,这意味着最多一个线程可以拥有该锁:
其中 lock 是一个模拟对象,仅用于锁定。
编辑:
这是一个为您提供的实现 - 不可重入互斥锁类,它使用值零表示解锁状态,使用值 1 表示锁定状态。
如果您需要知道应该在哪里调用
wait()
和notify()
,请查看sun.misc.Unsafe#park()
。它在 java.util.concurrent.locks 包中使用(AbstractQueuedSynchronizer <- LockSupport <- Unsafe)。希望这有帮助。
I think you're talking about mutex (or mutual exclusion locks). If so, you can use intrinsic locks. This kind of locks in Java act as mutexes, which means that at most one thread may own the lock:
Where lock is a mock object, used only for locking.
EDIT:
Here is an implementation for you — non-reentrant mutual exclusion lock class that uses the value zero to represent the unlocked state, and one to represent the locked state.
If you need to know where should you call
wait()
andnotify()
, have a look atsun.misc.Unsafe#park()
. It is used within java.util.concurrent.locks package (AbstractQueuedSynchronizer <- LockSupport <- Unsafe).Hope this helps.
这是我为二进制信号量所做的一个简单实现:
该实现具有二进制信号量的一个属性,您无法通过对只有一个许可的信号量进行计数来获得该属性 - 多次调用release仍将只留下一个可用资源。 此处提到了此属性。
Here is a simple implementation I did for a binary semaphore:
This implementation has one property of binary semaphores that you cannot get with counting semaphores that only have one permit - multiple calls to release will still leave just one resource available. This property is mentioned here.
是的,你可以。具有单个许可的信号量是二进制信号量。它们控制对单个资源的访问。它们可以被视为某种互斥锁/锁。
Yes, you can. A semaphore with a single permit is a binary semaphore. They control access to a single resource. They can be viewed as some kind of a mutex/lock.
这里直接来自 Java 站点
我认为,使用 Semaphore 类等更高级别抽象的全部原因是您不必调用低级别
wait
/notify
。Here is straight from the Java site
I think, the whole reason of using higher-level abstracts such as Semaphore class is that you don't have to call low level
wait
/notify
.我在 Java 中有自己的二进制信号量实现。
如果您在代码中发现任何错误,请告诉我,但到目前为止它总是工作正常! :)
I have my own implementation of a Binary Semaphore in Java.
Tell me if you find any bug in the code please, but so far it always worked fine! :)
我宁愿使用 Lock class
除了命名匹配之外,Java Semaphore 无法实现 BinarySemaphore,并且使用 Object 等待/通知或同步非常原始。
相反,Lock 类通过其锁定/解锁(与信号量的获取/释放)提供了与 Semaphore 几乎相同的锁定语义,但它专门用于解决临界区功能,其中一次只需要一个线程进入。
值得注意的是,由于 tryLock 方法。
I would rather use the Lock class
Besides the naming matching, Java Semaphore is no way to implement a BinarySemaphore, and using Object wait/notify or synchronize is quite raw.
Instead, the Lock class provides almost the same locking semantics as a Semaphore with its lock/unlock (versus acquire/release by Semaphore), but it is specifically targeted to solve critical section functionality, where just one thread is expected to enter at once.
Worth noting Lock also provide try with timeout semantics thanks to tryLock method.
也许使用 AtomicBoolean 实现它是一个好主意。
如果不是,请告诉我。
Maybe it's a good idea to use AtomicBoolean implement it.
If it's not, please let me know.
您可以查看 Semaphore 类(或者直接使用它?)
You could have a look at the source code for the Java implementation of the Semaphore class (or perhaps use it directly?)