How to do it...

Follow these steps to implement the example:

  1. Create a class named MyPriorityTask that implements the Runnable and Comparable interfaces parameterized by the MyPriorityTask class interface:
        public class MyPriorityTask implements Runnable,                              Comparable<MyPriorityTask> {
  1. Declare a private int attribute named priority:
        private int priority;
  1. Declare a private String attribute called name:
        private String name;
  1. Implement the constructor of the class to initialize its attributes:
        public MyPriorityTask(String name, int priority) {           this.name=name;           this.priority=priority;         }
  1. Implement a method to return the value of the priority attribute:
        public int getPriority(){           return priority;         }
  1. Implement the compareTo()

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.