Need help understanding Thread wait and notify

class Semaphore {
   private int count=100;
   public Semaphore(int n) {
      this.count = n;
   }

   public synchronized void acquire() {
      while(count == 0) {
         try {
            wait();
         } catch (InterruptedException e) {
            //keep trying
         }
      }
      count--;
   }

   public synchronized void release() {
      count++;
      notify(); //alert a thread that blocking on this semaphore
   }
}

I currently support 100 users. If the request comes from jsp (Client) and passes through this class, will there be Thread (request from JSP) waitand notifyautomatically?

+5
source share
2 answers

I highly recommend using java.util.concurrent.Semaphore from the standard library instead of writing a custom implementation of Semaphore. Just to mention one of the reasons why this is generally better: it notifydoes not give any FIFO guarantees (from Object.notify () ):

The choice is arbitrary and is at the discretion of the implementation

+6

.

Java 6 Semaphore. tutorial .

+1

All Articles