在Java中增加信号量允许

时间:2012-03-20 14:50:04

标签: java multithreading concurrency semaphore

我可以在Java中为信号量添加更多许可吗?

Semaphore s = new Semaphore(3);

在代码中的某处后,我想将许可证更改为4.这可能吗?

1 个答案:

答案 0 :(得分:12)

是。 release方法(容易混淆地命名为imo)可以用来增加许可证,因为从文档:

There is no requirement that a thread that releases a permit must 
have acquired that permit by calling acquire. 
Correct usage of a semaphore is established by programming convention
in the application. 

换句话说:

semaphore.release(10);

如果线程调用没有获得任何许可,将增加10个许可。