How it works...

Converting a regular executor into a priority-based executor is simple. You only have to pass a PriorityBlockingQueue object, parameterized by the Runnable interface, as a parameter. But with the executor, you should know that all the objects stored in a priority queue have to implement the Comparable interface.

You implemented the MyPriorityTask class that implements the Runnable interface, which will act as a task, and the Comparable interface to be stored in the priority queue. This class has a Priority attribute that is used to store the priority of the tasks. If a task has a higher value for this attribute, it will be executed earlier. The compareTo() method determines the order of the tasks in the priority queue. In ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.