首页 > 代码库 > Semaphore
Semaphore
Before obtaining an item each thread must acquire a permit from the semaphore, guaranteeing that an item is available for use.
When the thread has finished with the item it is returned back to the pool and a permit is returned to the semaphore, allowing
another thread to acquire that item. Note that no synchronization lock is held when acquire()
is called as that would prevent an
item from being returned to the pool. The semaphore encapsulates the synchronization needed to restrict access to the pool, s
eparately from any synchronization needed to maintain the consistency of the pool itself.
获得一项前,每个线程必须从信号量获取许可,从而保证可以使用该项。该线程结束后,将项返回到池中并将许可返回到该信号量,从而允许其他线
程获取该项。注意,调用 acquire()
时无法保持同步锁,因为这会阻止将项返回到池中。信号量封装所需的同步,以限制对池的访问,这同维持该池
本身一致性所需的同步是分开的。
A semaphore initialized to one, and which is used such that it only has at most one permit available, can serve as a mutual
exclusion lock. This is more commonly known as a binary semaphore, because it only has two states: one permit available,
or zero permits available. When used in this way, the binary semaphore has the property (unlike many Lock
implementations),
that the "lock" can be released by a thread other than the owner (as semaphores have no notion of ownership). This can be
useful in some specialized contexts, such as deadlock recovery.
将信号量初始化为 1,使得它在使用时最多只有一个可用的许可,从而可用作一个相互排斥的锁。这通常也称为二进制信号量,因为它只能有两种状态:
一个可用的许可,或零个可用的许可。按此方式使用时,二进制信号量具有某种属性(与很多 Lock
实现不同),即可以由线程释放“锁”,而不是由所有
者(因为信号量没有所有权的概念)。在某些专门的上下文(如死锁恢复)中这会很有用。
The constructor for this class optionally accepts a fairness parameter. When set false, this class makes no guarantees about the
order in which threads acquire permits. In particular, barging is permitted, that is, a thread invoking acquire()
can be allocated
a permit ahead of a thread that has been waiting - logically the new thread places itself at the head of the queue of waiting threads.
When fairness is set true, the semaphore guarantees that threads invoking any of the acquire
methods are selected to obtain
permits in the order in which their invocation of those methods was processed (first-in-first-out; FIFO). Note that FIFO ordering
necessarily applies to specific internal points of execution within these methods. So, it is possible for one thread to invoke acquire
before another, but reach the ordering point after the other, and similarly upon return from the method. Also note that the untimed
tryAcquire
methods do not honor the fairness setting, but will take any permits that are available.
此类的构造方法可选地接受一个公平 参数。当设置为 false 时,此类不对线程获取许可的顺序做任何保证。特别地,闯入 是允许的,也就是说可以在
已经等待的线程前为调用 acquire()
的线程分配一个许可,从逻辑上说,就是新线程将自己置于等待线程队列的头部。当公平设置为 true 时,信号量
保证对于任何调用获取
方法的线程而言,都按照处理它们调用这些方法的顺序(即先进先出;FIFO)来选择线程、获得许可。注意,FIFO 排序必然应
用到这些方法内的指定内部执行点。所以,可能某个线程先于另一个线程调用了 acquire
,但是却在该线程之后到达排序点,并且从方法返回时也类似。
还要注意,非同步的 tryAcquire
方法不使用公平设置,而是使用任意可用的许可。
Generally, semaphores used to control resource access should be initialized as fair, to ensure that no thread is starved out from
accessing a resource. When using semaphores for other kinds of synchronization control, the throughput advantages of non-fair
ordering often outweigh fairness considerations.
通常,应该将用于控制资源访问的信号量初始化为公平的,以确保所有线程都可访问资源。为其他的种类的同步控制使用信号量时,非公平排序的吞
吐量优势通常要比公平考虑更为重要。
This class also provides convenience methods to acquire
and release
multiple permits at a time. Beware of the increased risk of
indefinite postponement when these methods are used without fairness set true.
这个类还提供便捷的方法来同时 acquire
和 release
多个许可。注意在未将公平设置为 true 时使用这些方法会增加无限延期的风险。
Memory consistency effects: Actions in a thread prior to calling a "release" method such as release()
happen-before actions following
a successful "acquire" method such as acquire()
in another thread.
内存一致性效果:线程中调用“释放”方法(比如 release()
)之前的操作 happen-before 另一线程中紧跟在成功的“获取”方法(比如 acquire()
)之后的操作。
Semaphore