java 编程思想中的PriorityBlockingQueue 部分里对PrioritizedTask的排序没有显式调用collection的sort方法。而是通过take时做的这个动作
2016-04-29 09:40
507 查看
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).
至此我个人的疑惑
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).
至此我个人的疑惑
相关文章推荐
- Flex开发实战(二)--Flash Builder 4.7下载和安装及破解方法
- UISlider滑动问题
- SURVIVAL SHOOTER TUTORIAL之Player Health & UI & Enemy Attack [6]
- STL queue队列容器和priority_queue优先队列容器
- EasyUI扩展 datagrid列名包含特殊字符会导致表格错位(5)
- executeQuery 、execute与executeUpdate区别
- requests 请求
- easyui扩展:datagrid鼠标经过提示单元格内容(6)
- 【论文笔记】Reading Scene Text in Deep Convolutional Sequences
- 进击的KFC:在同一个Xib内创建多个UITableViewCell来方便使用
- mac Cornerstone SVN出错 Description : The working copy is locked due to a previous error
- PhotoUI框架的添加导致的iOS7及之前版本崩溃问题
- [Leetcode刷题总结系列][Dynamic programming][Divide-and-conquer]53.Maximum Subarray
- iOS CoreBluetooth 教程
- 双向队列 Deque
- STL deque双端队列容器
- 对石家庄铁道大学网站的UI分析
- 为什么Java中1000==1000为false而100==100为true?
- iOS 获取UIView 动画的实时位置的方法
- HDU 4027 Can you answer these queries?(线段树、查询区间和及修改区间数为数的平方根)