title: Java semaphore increases the number of permits when I do release more than number of the acquires
body: <p>I'm learning about Mutex and Semaphore in Java. So I thought of getting my hands dirty.</p>
  
  <p>I understand that mutex is semaphore with single permit from this <a href="https://www.mkyong.com/java/java-thread-mutex-and-semaphore-example/" rel="nofollow noreferrer">link</a> and mutex has the concept of ownership from this <a href="https://stackoverflow.com/questions/771347/what-is-mutex-and-semaphore-in-java-what-is-the-main-difference">link</a>.</p>
  
  <p>To demonstrate ownership I wrote below program and found below output.
  When I do release more than acquires, it actually increases the number of permits.</p>
  
  <p>Below is the program.</p>
  
  <pre><code>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(() -&gt; 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(() -&gt; es.shutdownNow()));
          System.out.println(es.isShutdown());
      }
  }
  </code></pre>
  
  <p>The output is - </p>
  
  <pre><code>mutexWait.isDone() true
  successfully released permits for mutex 
  available permits are 1
  available permits are 2
  available permits are 3
  false
  </code></pre>
  
  <p>Is this behavior expected. If so, how this works</p>
  
  <p>My java installation details -</p>
  
  <pre><code>$ 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)
  </code></pre>