Search code examples
javapriority-queueblockingqueue

How to add poison pill in PriorityBlockingQueue in java?


Usually for ArrayBlockingQueue we will add poison pill in finally block and it will be at the end of the queue. So whenever we are taking it out we can identify if poison pill comes that is end of queue. But here PriorityBlockingQueue is relying on natural ordering, so How can I handle poison pill? What is best practice?


Solution

  • Implement a custom java.util.Comparator that tests for your poison pill and always returns -1 when it finds it then pass an instance of the custom Comparator to the PriorityBlockingQueue in the constructor.