title: How to implement fair usage policy of threads in API backend?
body: <p>I am building an REST API in Java which I would be exposing to the outside world. People who would be invoking the API would have to be registered and would be sending their userid in the request. 
  There would be a maximum of, say, 10 concurrent threads available for executing the API request. I am maintaining a queue which holds all the request ids to be serviced (the primary key of the DB entry).
  I need to implement some fair usage policy as follows - 
  If there are more than 10 jobs in the queue (i.e more than max number of threads), a user is allowed to execute only one request at a time (the other requests submitted by him/her, if any, would remain in the queue and would be taken up only once his previous request has completed execution). In case there are free threads, i.e. even after allotting threads to requests submitted by different users, then the remaining threads in the thread pool can be distributed among the remaining requests (even if the user who has submitted the request is already holding one thread at that moment).</p>
  
  <p>The current implementation is as follows - </p>
  
  <pre><code>import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import java.util.concurrent.PriorityBlockingQueue;
  import java.util.concurrent.Semaphore;
  
  
  public class APIJobExecutor implements Runnable{
      private static PriorityBlockingQueue&lt;Integer&gt; jobQueue = new PriorityBlockingQueue&lt;Integer&gt;();
      private static ExecutorService jobExecutor = Executors.newCachedThreadPool();
      private static final int MAX_THREADS = 10;
      private static Semaphore sem = new Semaphore(MAX_THREADS, true);
  
      private APIJobExecutor(){
  
      }
  
      public static void addJob(int jobId)
      {
          if(!jobQueue.contains(jobId)){
              jobQueue.add(new Integer(jobId));
          }
      }
  
      public void run()
      {
          while (true) {
              try {
                  sem.acquire();
              }catch (InterruptedException e1) {
                  e1.printStackTrace();
                  //unable to acquire lock. retry.
                  continue;
              }
              try {
                  Integer jobItem = jobQueue.take();
                  jobExecutor.submit(new APIJobService(jobItem));
              } catch (InterruptedException e) {
                  e.printStackTrace();
              } finally {
                  sem.release();
              }
          }
      }
  }
  </code></pre>
  
  <p>Edit:
  Is there any out of the box Java data structure that gives me this functionality. If not, how do I go about implementing the same?</p>