java 编程思想中的PriorityBlockingQueue 在take是会调用comparable 的compareTo进行排序,故不需要显式调用collection的sort方法
这个方法是PriorityBlockingQueue的:
public E take() throws InterruptedException {
ReentrantLock localReentrantLock = this.lock;
localReentrantLock.lockInterruptibly();
try {
try {
while (this.q.size() == 0)
this.notEmpty.await();
} catch (InterruptedException localInterruptedException) {
this.notEmpty.signal();
throw localInterruptedException;
}
Object localObject1 = this.q.poll();//调用了poll方法并对poll进行了判空,这个poll是PriorityQueue的poll
assert (localObject1 != null);
return localObject1;
} finally {
localReentrantLock.unlock();
}
}
PriorityQueue的poll方法:
public E poll() {
if (this.size == 0)
return null;
int i = --this.size;
this.modCount += 1;
Object localObject1 = this.queue[0];
Object localObject2 = this.queue[i];
this.queue[i] = null;
if (i != 0)
siftDown(0, localObject2);//调用了内部的siftDown进行排序
return localObject1;
}
private void siftDown(int paramInt, E paramE)
{
if (this.comparator != null)
siftDownUsingComparator(paramInt, paramE);
else
siftDownComparable(paramInt, paramE);//java编程思想用的是comparable.
}
private void siftDownComparable(int paramInt, E paramE) {
Comparable localComparable = (Comparable)paramE;
int i = this.size >>> 1;
while (paramInt < i) {
int j = (paramInt << 1) + 1;
Object localObject = this.queue[j];
int k = j + 1;
if ((k < this.size) && (((Comparable)localObject).compareTo(this.queue[k]) > 0))
{
localObject = this.queue[(j = k)];
}if (localComparable.compareTo(localObject) <= 0)//调用了添加到PriorityBlockingQueue的元素的compareTo方法,java编程思想是重写了comparable的compareTo
break;
this.queue[paramInt] = localObject;
paramInt = j;
}
this.queue[paramInt] = localComparable;
}
java 编程思想的PrioritizedTask继承并重写了compareTo,详情请参考java编程思想4版728页。所以在本书中得到例子并没有调用Collections.sort(list).
至此我个人的疑惑