以下代码通过编译是合法的。为什么可以将 PriorityQueue 定义为仅获取 instanceof Comparable 的元素?
...
PriorityQueue<Object> q = new PriorityQueue<Object>();
q.add(new Object());
...
但它抛出了预期的异常:
Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.Comparable
at java.util.PriorityQueue.siftUpComparable(PriorityQueue.java:595)
at java.util.PriorityQueue.siftUp(PriorityQueue.java:591)
at java.util.PriorityQueue.offer(PriorityQueue.java:291)
at java.util.PriorityQueue.add(PriorityQueue.java:268)
at ReentrantLockExample.main(ReentrantLockExample.java:12)