b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

链表 结构 解读 Java 并发队列 BlockingQueue(11)

电脑杂谈  发布时间:2018-02-21 23:42:01  来源:网络整理

this.notEmpty = lock.newCondition();

//

boolean heapify = true; // true if not known to be in heap order

boolean screen = true; // true if must screen for nulls

if (c instanceof SortedSet<?>) {

SortedSet<? extends E> ss = (SortedSet<? extends E>) c;

this.comparator = (Comparator<? super E>) ss.comparator();

heapify = false;

}

else if (c instanceof PriorityBlockingQueue<?>) {

PriorityBlockingQueue<? extends E> pq =

(PriorityBlockingQueue<? extends E>) c;

this.comparator = (Comparator<? super E>) pq.comparator();

screen = false;

if (pq.getClass() == PriorityBlockingQueue.class) // exact match

heapify = false;

}

Object[] a = c.toArray();

int n = a.length;

// If c.toArray incorrectly doesn't return Object[], copy it.

if (a.getClass() != Object[].class)

a = Arrays.copyOf(a, n, Object[].class);

if (screen && (n == 1 || this.comparator != null)) {

for (int i = 0; i < n; ++i)

if (a[i] == null)

throw new NullPointerException();

}

this.queue = a;

this.size = n;

if (heapify)

heapify();

}

接下来,我们来看看其内部的自动扩容实现:

private void tryGrow(Object[] array, int oldCap) {

// 这边做了释放锁的操作

lock.unlock(); // must release and then re-acquire main lock

Object[] newArray = null;

// 用 CAS 操作将 allocationSpinLock 由 0 变为 1,也算是获取锁

if (allocationSpinLock == 0 &&

UNSAFE.compareAndSwapInt(this, allocationSpinLockOffset,

0, 1)) {

try {

// 如果节点个数小于 64,那么增加的 oldCap + 2 的容量

// 如果节点数大于等于 64,那么增加 oldCap 的一半

// 所以节点数较小时,增长得快一些

int newCap = oldCap + ((oldCap < 64) ?


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-87792-11.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 叶龙飞
      叶龙飞

      布什布莱尔无缘无故入侵伊拉克害死了数万伊拉克百姓

    • 鲁桓公姬允
      鲁桓公姬允

      反而给中国扶持出强大的敌人

    热点图片
    拼命载入中...