Cover by Javier Fernández González

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

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() ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required