title: ExecutorService with PriorityBlockingQueue and Implementing Comparator
body: <p>I am building a REST Service and I have the following structure of classes:</p>
  
  <pre><code>public class MyTask implements Runnable {
  
     @Override
      public void run() {
         //do stuff
      }
  }
  
  public class RestController  {
  
     private MyThreadPool myThreadPool;
  
     @RequestMapping(value = "/{id}", method = RequestMethod.POST)
     public ResponseEntity&lt;?&gt; getSomething (....){
      myThreadPool.getExecutorService().submit(new MyTask(request));
     }
  
  
  }
  
  
  public class MyThreadPool{
  
      private final static Integer SIZE_OF_POOL = 5;
      private final static Integer KEEP_ALIVE= 5000;
      private final static Integer QUEUE_SIZE = 50;
      private final static Integer MAX_POOL_SIZE = 10;
  
  
      private BlockingQueue&lt;Runnable&gt; queue = new PriorityBlockingQueue&lt;Runnable&gt;(QUEUE_SIZE); //pass comparator here? 
      private ExecutorService executorService = new ThreadPoolExecutor(SIZE_OF_POOL, MAX_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, queue, new ThreadPoolExecutor.CallerRunsPolicy());
  
      public ExecutorService getExecutorService() {
          return executorService;
      }
  }
  </code></pre>
  
  <p>I may have scenarios in my project where my <code>request</code> object has an integer to represent <strong>priority</strong>.</p>
  
  <p>I am looking to Submit runnable tasks for execution with this priority value and I am not sure where to implement this logic and how to do it. I know I have to use the <code>PriorityBlockingQueue</code> somewhere and implement a <code>Comparator</code>. So do I...</p>
  
  <p>1) Implement in MyThreadPool class
  2) Or Implement logic in MyTask</p>
  
  <p>Can you guys please show how to implement this Comparator so that requests with highest priority are executed first? </p>