How to do it...

Follow these steps to implement the example:

  1. Create a class named Event and specify that it implements the Comparable interface parameterized by the Event class:
        public class Event implements Comparable<Event> {
  1. Declare a private int attribute named thread to store the number of threads that have created the event:
        private final int thread;
  1. Declare a private int attribute named priority to store the priority of the event:
        private final int priority;
  1. Implement the constructor of the class to initialize its attributes:
        public Event(int thread, int priority){ 
this.thread=thread;
this.priority=priority;
}
  1. Implement the getThread() method to return the value of the thread attribute:
        public int getThread() { 
return thread;
}
  1. Implement the getPriority() method to return the value of the priority attribute:
        public int getPriority() { 
return priority;
}
  1. Implement the compareTo() method. It receives Event as a parameter and compares the priority of the current event and the one received as a parameter. It returns -1 if the priority of the current event is bigger, 0 if both the priorities are equal, and 1 if the priority of the current event is smaller. Note that this is the opposite of most Comparator.compareTo() implementations:
        @Override 
public int compareTo(Event e) {
if (this.priority>e.getPriority()) {
return -1;
} else if (this.priority<e.getPriority()) {
return 1;
} else {
return 0;
}
}
  1. Create a class named Task and specify that it implements the Runnable interface:
        public class Task implements Runnable {
  1. Declare a private int attribute named id to store the number that identifies the task:
        private final int id;
  1. Declare a private PriorityBlockingQueue attribute parameterized by the Event class named queue to store the events generated by the task:
        private final PriorityBlockingQueue<Event> queue;
  1. Implement the constructor of the class to initialize its attributes:
        public Task(int id, PriorityBlockingQueue<Event> queue) { 
this.id=id;
this.queue=queue;
}
  1. Implement the run() method. It stores 1,000 events in the queue, using its ID, to identify the task that creates the event and we assign to each event a different priority from 1 to 1000. Use the add() method to store the events in the queue:
        @Override 
public void run() {
for (int i=0; i<1000; i++){
Event event=new Event(id,i);
queue.add(event);
}
}
  1. Implement the main class of the example by creating a class named Main and adding the main() method to it:
        public class Main{ 
public static void main(String[] args) {
  1. Create a PriorityBlockingQueue object parameterized by the Event class named queue:
        PriorityBlockingQueue<Event> queue=new PriorityBlockingQueue<>();
  1. Create an array of five Thread objects to store the threads that will execute the five tasks:
        Thread taskThreads[]=new Thread[5];
  1. Create five Task objects. Store the threads in the array created earlier:
        for (int i=0; i<taskThreads.length; i++){ 
Task task=new Task(i,queue);
taskThreads[i]=new Thread(task);
}
  1. Start the five threads created earlier:
        for (int i=0; i<taskThreads.length ; i++) { 
taskThreads[i].start();
}
  1. Wait for the finalization of the five threads using the join() method:
        for (int i=0; i<taskThreads.length ; i++) { 
try {
taskThreads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
  1. Write the actual size of the queue in the console and the events stored in it. Use the poll() method to take off the events from the queue:
        System.out.printf("Main: Queue Size: %d
",queue.size()); 
for (int i=0; i<taskThreads.length*1000; i++){
Event event=queue.poll();
System.out.printf("Thread %s: Priority %d ",
event.getThread(),event.getPriority());
}
  1. Write a message to the console with the final size of the queue:
        System.out.printf("Main: Queue Size: %d
",queue.size()); 
System.out.printf("Main: End of the program ");
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.179.220