如何知道信号量是否会阻塞我的进程

How to know if a semaphore is going to block my process

我正在 java 为我的大学做一个工作,我需要知道是否有一些方法可以知道信号量是否会阻止具有 aquire 的进程。

是的,如果您使用 java.util.concurrent.Semaphore:

boolean tryAcquire()

Acquires a permit from this semaphore, only if one is available at the time of invocation.

boolean acquired = semaphore.tryAcquire();
if (acquired) {
    System.out.println("Got it!");
    semaphore.release();
} else {
    System.out.println("Returned immediately, would have blocked");
}