您可以在执行 acquire() 的不同线程上调用 java.util.concurrent.Semaphore.release() 吗?

Can you call java.util.concurrent.Semaphore.release() on a different thread you did acquire() on?

我有一个 java 模块,它需要通过接口并发执行一组任务(为实现该模块的框架提供低级控制和处理),但同时不要太多,java.util.concurrent.Semaphore 似乎很适合这个。

我不确定的一个问题是 release()acquire() 调用的线程安全性。能否一个线程获取信号量锁,然后另一个线程释放它?

这是 Javadoc about release()

上的注释

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.

是的。你可以的。