title: PrioirtyBlockingQueue in Multithread application Java
body: <p>I have seen a very nice explanation by "thedarkpassenger" of how to implement PrioirtyQueue in multithread application however following only had one runnable class </p>
  
  <p>What if we have to implement multiple Runnable classes ?
  How to modify class MyFutureTask and main to accommodate multiple Runnable classes ? </p>
  
  <p>Driver class : This class creates an executor which accepts tasks and later submits them for execution. Here we create two tasks one with LOW priority and the other with HIGH priority. Here we tell the executor to run a MAX of 1 threads and use the PriorityBlockingQueue.</p>
  
  <pre><code>public static void main(String[] args) {
  
         /*
         Minimum number of threads that must be running : 0
         Maximium number of threads that can be created : 1
         If a thread is idle, then the minimum time to keep it alive : 1000
         Which queue to use : PriorityBlockingQueue
         */
      PriorityBlockingQueue queue = new PriorityBlockingQueue();
      ThreadPoolExecutor executor = new ThreadPoolExecutor(0,1,
          1000, TimeUnit.MILLISECONDS,queue);
  
      MyTask task = new MyTask(Priority.LOW,"Low");
      executor.execute(new MyFutureTask(task));
      task = new MyTask(Priority.HIGH,"High");
      executor.execute(new MyFutureTask(task));
  }
  </code></pre>
  
  <p>MyTask class : MyTask implements Runnable and accepts priority as an argument in the constructor. When this task runs, it prints a message and then puts the thread to sleep for 1 second.</p>
  
  <pre><code>public class MyTask implements Runnable {
  
    public int getPriority() {
      return priority.getValue();
    }
  
    private Priority priority;
  
    public String getName() {
      return name;
    }
  
    private String name;
  
    public MyTask(Priority priority,String name){
      this.priority = priority;
      this.name = name;
    }
  
    @Override
    public void run() {
      System.out.println("The following Runnable is getting executed "+getName());
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  
  }
  </code></pre>
  
  <p>MyFutureTask class : Since we are using PriorityBlocingQueue for holding our tasks, our tasks must be wrapped inside FutureTask and our implementation of FutureTask must implement Comparable interface. The Comparable interface compares the priority of 2 different tasks and submits the task with the highest priority for execution.</p>
  
  <pre><code> public class MyFutureTask extends FutureTask&lt;MyFutureTask&gt;
        implements Comparable&lt;MyFutureTask&gt; {
  
      private  MyTask task = null;
  
      public  MyFutureTask(MyTask task){
        super(task,null);
        this.task = task;
      }
  
      @Override
      public int compareTo(MyFutureTask another) {
        return task.getPriority() - another.task.getPriority();
      }
    }
  </code></pre>
  
  <p>Priority class : Self explanatory Priority class.</p>
  
  <pre><code>public enum Priority {
  
    HIGHEST(0),
    HIGH(1),
    MEDIUM(2),
    LOW(3),
    LOWEST(4);
  
    int value;
  
    Priority(int val) {
      this.value = val;
    }
  
    public int getValue(){
      return value;
    }
  
  
  }
  </code></pre>
  
  <p>Now when we run this example, we get the following output</p>
  
  <p>The following Runnable is getting executed High
  The following Runnable is getting executed Low</p>