O'Reilly logo

Java 7 Concurrency Cookbook 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

Using blocking thread-safe lists ordered by priority

A typical need when you work with data structures is to have an ordered list. Java provides PriorityBlockingQueue that has this functionality.

All the elements you want to add to PriorityBlockingQueue have to implement the Comparable interface. This interface has a method, compareTo() that receives an object of the same type, so you have two objects to compare: the one that is executing the method and the one that is received as a parameter. The method must return a number less than zero if the local object is less than the parameter, a number bigger that zero if the local object is greater than the parameter, and the number zero if both objects are equal.

PriorityBlockingQueue uses the compareTo() ...

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