Java 当我释放的数量多于获取的数量时,信号量会增加许可数量

Java semaphore increases the number of permits when I do release more than number of the acquires

我正在学习 Java 中的 Mutex 和 Semaphore。所以我想到了动手。

我从这个 link and mutex has the concept of ownership from this link.

了解到互斥锁是具有单一许可的信号量

为了证明所有权,我编写了以下程序并找到了以下输出。 当我释放的比获得的多时,它实际上增加了许可的数量。

下面是程序。

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;

public class MutexOwnerShipDemo {
    public static void main(String[] args) {
        Semaphore mutex = new Semaphore(1);
        final ExecutorService es = Executors.newSingleThreadExecutor();
        try {
            // acquire mutex lock permit in main thread
            mutex.acquire();

            // release the lock in different thread
            Future mutexWait = es.submit(() -> mutex.release());
            mutexWait.get();
            System.out.println("mutexWait.isDone() " + mutexWait.isDone() );
            System.out.println("successfully released permits for mutex \n " +
                "available permits are " + mutex.availablePermits());

            // release the lock in main thread
            mutex.release();
            System.out.println( "available permits are " + mutex.availablePermits());

            // release lock in main thread once again
            mutex.release();
            System.out.println( "available permits are " + mutex.availablePermits());
        } catch (Exception e) {

        }
        Runtime.getRuntime().addShutdownHook(new Thread(() -> es.shutdownNow()));
        System.out.println(es.isShutdown());
    }
}

输出是 -

mutexWait.isDone() true
successfully released permits for mutex 
available permits are 1
available permits are 2
available permits are 3
false

这种行为是否符合预期。如果是这样,这是如何工作的

我的java安装细节-

$ java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

是的,这是 the documentation 中引用的预期行为:

[...] Releases a permit, increasing the number of available permits by one. [...] There is no requirement that a thread that releases a permit must have acquired that permit by calling acquire().

您可以释放任意数量的许可:

Semaphore semaphore = new Semaphore(0);
semaphore.release(10); // it's fine
System.out.println(semaphore.availablePermits()); // 10

您无法获得随机数量的许可(准确地说,是超过当前可用许可数量的数量),但是:

Semaphore semaphore = new Semaphore(0);
semaphore.acquire(10); // you are blocked here

Semaphore semaphore = new Semaphore(0);
System.out.println(semaphore.tryAcquire(10)); // false

几件事:

  • 您正在使用 Semaphore,它可以有任意数量的许可,所以是的,这是意料之中的。见 Javadoc:

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.

  • 你没有使用多线程,因为你总是在 futures 中阻塞。您的代码是完全连续的。
  • 当人们说互斥体是具有单一许可的信号量时,他们谈论的是抽象概念,而不是 Java 实现。 Java 中的互斥量示例是 ReentrantLock,或与 synchronized 块一起使用的简单 Object