O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

In this example, you implemented a priority queue of Event objects using PriorityBlockingQueue. As mentioned in the introduction, all the elements stored in PriorityBlockingQueue have to implement the Comparable interface or provide a Comparator object to the constructor of the queue. In this case, you used the first approach, so you implemented the compareTo() method in the Event class.

All the events have a priority attribute. The elements that have a higher value of priority will be the first elements in the queue. When you implement the compareTo() method, if the event executing the method has a priority higher than the priority of the event passed as a parameter, it returns -1 as the result. In another case, if the event ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required