0

I need a thread pool that may receive tasks to be inserted either into the back of the queue (as usual), or into the front of the queue (for priority tasks). Tasks should then be executed normally, polled from the head of the queue.

I realize this means creating my own ThreadPoolExecutor using a BlockingDeque, but that's not enough... How do I actually tell the thread pool to call the queue's offerFirst method instead of offer?

Yuval
  • 7,987
  • 12
  • 40
  • 54
  • Can you share any code you've written for this ? It will help in making edits to put you on track – Aditya Jul 24 '19 at 09:37
  • 4
    maybe using a custom Comparator in addition with a BlockingQueue is an option for you? Something like BlockingQueue queue=new PriorityBlockingQueue(2,new CustomTaskComparator()); and then have compareTo(Task a, Task b) in the CustomTaskComparator? – ItFreak Jul 24 '19 at 09:43
  • 1
    Duplicate of https://stackoverflow.com/questions/3198660/java-executors-how-can-i-set-task-priority and https://stackoverflow.com/questions/807223/how-do-i-implement-task-prioritization-using-an-executorservice-in-java-5 ? – Thilo Jul 24 '19 at 10:06

1 Answers1

2

You need to pass the PriorityBlockingQueue to the ThreadPoolExecutor, probably using this constructor. Added example below of how to initialise the PriorityBlockingQueue with a comparator

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue) {
    this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
         Executors.defaultThreadFactory(), defaultHandler);
}

Edit - Example to add a comparator to your priority queue

import java.util.Comparator;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class PriorityBlockQueueTest {

    private static BlockingQueue<PriorityTask> taskQueue = new PriorityBlockingQueue<>(10,
            new Comparator<PriorityTask>() {
        @Override
        public int compare(PriorityTask o1, PriorityTask o2) {
            return o2.getData() - o1.getData();
        }
    });

    public static void main(String arg[]) {
        taskQueue.add(new PriorityTask(2, 10));
        taskQueue.add(new PriorityTask(1, 11));

        System.out.println(taskQueue);
    }

    private static class PriorityTask implements  Runnable {
        private int priority;
        private int data;
        public PriorityTask(int priority, int data) {
            this.priority = priority;
            this.data = data;
        }

        public int getData() {
            return data;
        }

        public void run() {
            System.out.println("Running something");
        }

        public String toString() {
            return "priority: " + priority + " data: " + data;
        }
    }
}
Aditya
  • 2,148
  • 3
  • 21
  • 34
  • Building the thread pool with my own queue is easy... My question is how to set a priority for the tasks, if they are simple `Runnable`s? – Yuval Jul 25 '19 at 06:48
  • 1
    The PriorityBlockingQueue lets you specify a comparator. Add the implementation of checking within the comparator – Aditya Jul 25 '19 at 07:07
  • Added example of the queue. Typecast as you see fit – Aditya Jul 25 '19 at 07:21