Re: Somewhere in this constructor of PriorityBlockingQueue is hard to understand
Jason Mehrens via Concurrency-interest <[email protected]> Wed, 5 Aug 2020 04:04:12 +0000
| Newsgroups | gmane.comp.java.jsr.166-concurrency |
|---|---|
| Message-ID | <DM5PR1801MB20749544C3ED11E920D13F82834B0@DM5PR1801MB2074.namprd18.prod.outlook.com> |
Holder violates the spec of https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Comparable.html. Per the docs: "Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false" The bug is not in PQ it is in the 3rd party Holder implementation. Jason ________________________________________ From: Liu <[email protected]> Sent: Tuesday, August 4, 2020 9:07 PM To: Jason Mehrens Cc: Concurrency-interest Subject: Re:Re: [concurrency-interest] Somewhere in this constructor of PriorityBlockingQueue is hard to understand import java.util.ArrayList; import java.util.Queue; import java.util.concurrent.PriorityBlockingQueue; public class test16 { static class Holder implements Comparable<Holder> { int i; Holder(int i) { this.i = i; } public int compareTo(Holder h) { if(h == null) return -1; return this.i - h.i; } } public static void main(String[] args) { ArrayList<Holder> al = new ArrayList<>(2); al.add(new Holder(1)); al.add(new Holder(2)); al.add(null); Queue a = new PriorityBlockingQueue<>(al); } } In this case, heapify() cound not find null item. _______________________________________________ Concurrency-interest mailing list [email protected] http://cs.oswego.edu/mailman/listinfo/concurrency-interest