java 用 1 和 0 初始化的信号量之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25563640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:14:00  来源:igfitidea点击:

Difference between Semaphore initialized with 1 and 0

javajava.util.concurrent

提问by Atul Kumar

Please tell what is difference Semaphore initialized with 1 and zero. as below:

请说出用 1 和零初始化的信号量有何不同。如下:

public static Semaphore semOne = new Semaphore(1);

and

public static Semaphore semZero = new Semaphore(0);

回答by Chip

The argument to the Semaphore instance is the number of "permits" that are available. It can be any integer, not just 0 or 1.

Semaphore 实例的参数是可用的“许可”数量。它可以是任何整数,而不仅仅是 0 或 1。

For semZeroall acquire()calls will block and tryAcquire()calls will return false, until you do a release()

因为semZero所有acquire()调用都将阻塞并且tryAcquire()调用将返回 false,直到您执行release()

For semOnethe first acquire()calls will succeed and the rest will block until the first one releases.

因为semOne第一个acquire()调用将成功,其余的将阻塞,直到第一个调用释放。

The class is well documented here.

该类在此处有详细记录。

Parameters: permits - the initial number of permits available. This value may be negative, in which case releases must occur before any acquires will be granted.

参数: permit - 可用的初始许可数。该值可能为负数,在这种情况下,必须在授予任何获取之前进行释放。

回答by Joop Eggen

the constructor parameter permits(initial semaphore counter) is the number of calls to Semaphore.aquire()that can be made before the counter (permits) is zero, and the acquire()blocks.

构造函数参数permits(初始信号量计数器)是Semaphore.aquire()在计数器(许可)为零之前可以进行的调用次数,以及acquire()块。

1is a normal value, to ensure that only one thread passes the acquire.

1是正常值,保证只有一个线程通过acquire。

semaphore.acquire();
try {
    // Critical region
    ...
} finally {
    semaphore.release();
}

For a usage of 0see here.

有关0的用法,请参见此处

Semaphoreis a low-level mechanism for concurrency: a counter when reaching zero blocking thread execution. It stems from Dijkstrawhere the binary semaphore (0, 1) is a metaphore for a railway semaphore saying pass (halt when 0, pass --permits) and at the end of the protected track does a release (++permits).

信号量是一种低级并发机制:当线程执行达到零阻塞时的计数器。它源自Dijkstra,其中二进制信号量 (0, 1) 是铁路信号量的隐喻,表示 pass(0 时停止,pass --permits),并在受保护轨道的末尾释放(++permits)。

回答by Morten

When I first read the documentation of the Semaphore I also misinterpreted the explanation. The main point I missed was the part of '...INITIAL number of permits...'. Somehow I though this would be the number of permits which would be available as a MAXIMUM, but that is NOT the case. In the end the semaphore just counts up from any number but only starts the enable waiting threads (which use the acquire) when the semaphore permits are above 1.

当我第一次阅读信号量的文档时,我也误解了解释。我错过的主要观点是“...初始许可数量...”的部分。不知何故,我虽然这将是可作为最大数量的许可证数量,但事实并非如此。最后,信号量只是从任何数字开始计数,但仅当信号量允许大于 1 时才启动启用等待线程(使用获取)。

A simple piece of (non threading) code shows this too:

一段简单的(非线程)代码也显示了这一点:

@Test
public void testNegativeSemaphore()
{
    Semaphore semaphore = new Semaphore(-2);

    assertEquals(-2, semaphore.availablePermits());
    semaphore.release();
    assertEquals(-1, semaphore.availablePermits());
    semaphore.release();
    assertEquals(0, semaphore.availablePermits());
    semaphore.release();
    assertEquals(1, semaphore.availablePermits());
}

As the code shows, the available permits increase upon each release, only to allow other threads to acquire a permit once the value reaches 1 or above.

如代码所示,每次发布时可用的许可都会增加,只有在值达到 1 或更高时才允许其他线程获取许可。

NOTE that from there on the availablePermits cannot become negative by using the aqcuire because if there are 0 permits the whole point of the semaphore is to wait for a permit to become available again!

请注意,从那里开始,使用 aqcuire 不能将 availablePermits 变为负数,因为如果有 0 个许可,则信号量的全部意义在于等待许可再次变为可用!