当前位置: 首页 > news >正文

广州网站建设费用多少网络营销的认知

广州网站建设费用多少,网络营销的认知,昆山做网站公司有哪些,泰安选择企业建站公司文章目录 1 PriorityBlockingQueue是什么2 核心属性详解3 核心方法详解3.1 offer(E e)3.2 poll()3.3 take()3.4 peek() 4 总结 1 PriorityBlockingQueue是什么 PriorityBlockingQueue类上的注释描述:一个无界阻塞队列,它使用与类PriorityQueue相同的排序…

文章目录

  • 1 PriorityBlockingQueue是什么
  • 2 核心属性详解
  • 3 核心方法详解
    • 3.1 offer(E e)
    • 3.2 poll()
    • 3.3 take()
    • 3.4 peek()
  • 4 总结


1 PriorityBlockingQueue是什么

PriorityBlockingQueue类上的注释描述:一个无界阻塞队列,它使用与类PriorityQueue相同的排序规则,并提供阻塞检索操作。
PriorityQueue又是什么:基于优先级的 堆 的无限制优先级队列,PriorityQueue是一个小顶堆。

2 核心属性详解

利用数组实现小顶堆,利用ReentrantLock 保证元素插入和移除的线程安全。同时因为是无界队列,所以需要扩容机制,此时引入遍历allocationSpinLock 对他unsafe的cas操作,表示谁去扩容。

	//元素存放的集合容器,堆结构也是个数组,所以需要数组集合private transient Object[] queue;//元素的数量private transient int size;//指定的元素的比较规则private transient Comparator<? super E> comparator;//保证线程安全的锁private final ReentrantLock lock;//当获取元素的线程因为集合中无元素而阻塞,会使用该等待条件去实现private final Condition notEmpty;/*** 扩容时候使用的cas锁,因为扩容要保证线程安全,数组扩容是要new一个新数组的*/private transient volatile int allocationSpinLock;//序列化和反序列化使用的private PriorityQueue<E> q;

3 核心方法详解

3.1 offer(E e)

put操作一样,因为无界队列,所以没有存在容量满了,阻塞等待获取元素的线程唤醒
整体逻辑就是元素放入堆中,如果容量不够就进行扩容,ReentrantLock 保证了这些操作是线程安全的

    public boolean offer(E e) {if (e == null)throw new NullPointerException();//获取锁final ReentrantLock lock = this.lock;lock.lock();int n, cap;Object[] array;//如果当前元素数量已经达到了数组的长度上限,则需要扩容while ((n = size) >= (cap = (array = queue).length))//对当前数组进行扩容 扩容代码下面有解释tryGrow(array, cap);try {Comparator<? super E> cmp = comparator;// 如果没指定比较器,就代表你的类实现了Comparable接口if (cmp == null)//添加元素到堆里 堆排序算法siftUpComparable(n, e, array);else//使用指定的比较器进行入堆siftUpUsingComparator(n, e, array, cmp);size = n + 1;notEmpty.signal();} finally {lock.unlock();}return true;}//扩容代码描述private void tryGrow(Object[] array, int oldCap) {//先释放锁,因为当前线程需要干扩容的活,不需要阻塞别的线程,这样可能别的线程执行到这扩容已经好了,//就可以执行if (newArray != null && queue == array) ture成立的条件了lock.unlock();Object[] newArray = null;//通过扩容锁,减小锁的粒度,只有一个线程能去开辟新的数组if (allocationSpinLock == 0 &&UNSAFE.compareAndSwapInt(this, allocationSpinLockOffset,0, 1)) {try {//当前数组长度小于64的时候长度加2,否则长度翻倍int newCap = oldCap + ((oldCap < 64) ?(oldCap + 2) : // grow faster if small(oldCap >> 1));//长度到达上限抛异常                      if (newCap - MAX_ARRAY_SIZE > 0) {// possible overflow//MAX_ARRAY_SIZE 是int最大值减8 此时还能慢慢的加,不过一般来说都会OOM了int minCap = oldCap + 1;if (minCap < 0 || minCap > MAX_ARRAY_SIZE)throw new OutOfMemoryError();newCap = MAX_ARRAY_SIZE;}//if (newCap > oldCap && queue == array)newArray = new Object[newCap];} finally {allocationSpinLock = 0;}}//此时可能线程没拿到扩容锁,且newArray = new Object[newCap];还没执行到if (newArray == null)Thread.yield();//再次获取主锁lock.lock();//此时两种情况//1.到这newArray = new Object[newCap];还没执行到,此时newArray == null,queue还没被替换,所以该方法结束之后,循环又回来了,释放锁。。。再次争抢锁//2.newArray = new Object[newCap];已执行,此时因为获取的是主锁,只有一个线程能执行底下的queue = newArray;和数组copy操作//这样,其他线程获取到锁之后就会往新的数组中添加元素了if (newArray != null && queue == array) {queue = newArray;System.arraycopy(array, 0, newArray, 0, oldCap);}}

如果队列中数组元素特别多,此时扩容一次需要的时间就会相对增加,其他线程阻塞的时间就会边长。

3.2 poll()

获取一个元素。

    public E poll() {//获取锁final ReentrantLock lock = this.lock;lock.lock();try {//拿出堆顶元素 return dequeue();} finally {lock.unlock();}}private E dequeue() {//n是当前元素结合中左后一个元素int n = size - 1;if (n < 0)return null;else {Object[] array = queue;//堆顶就是0下标E result = (E) array[0];E x = (E) array[n];array[n] = null;//堆顶被取出,此时把末尾元素拿上来去重新比较排序保证堆的完整性Comparator<? super E> cmp = comparator;if (cmp == null)siftDownComparable(0, x, array, n);elsesiftDownUsingComparator(0, x, array, n, cmp);size = n;return result;}}

3.3 take()

相对于poll 多了等待操作

public E take() throws InterruptedException {final ReentrantLock lock = this.lock;lock.lockInterruptibly();E result;try {while ( (result = dequeue()) == null)//区别在这notEmpty.await();} finally {lock.unlock();}return result;
}

3.4 peek()

获取堆顶元素,但不移除

 public E peek() {//获取锁final ReentrantLock lock = this.lock;lock.lock();try {//返回堆顶元素return (size == 0) ? null : (E) queue[0];} finally {lock.unlock();}}

4 总结

PriorityBlockingQueue是一个小顶堆的数据结构的类,使用了ReentrantLock来保证线程安全。可以通过传入的比较器去自定义小顶堆的比较规则,或者实现Comparable接口。


文章转载自:
http://unending.xtqr.cn
http://gls.xtqr.cn
http://rhinosporidiosis.xtqr.cn
http://catawampus.xtqr.cn
http://shekel.xtqr.cn
http://ought.xtqr.cn
http://publicise.xtqr.cn
http://khidmatgar.xtqr.cn
http://septenate.xtqr.cn
http://submergible.xtqr.cn
http://grundy.xtqr.cn
http://photoelement.xtqr.cn
http://whichsoever.xtqr.cn
http://horoscopic.xtqr.cn
http://syphilitic.xtqr.cn
http://ringlet.xtqr.cn
http://radiance.xtqr.cn
http://tailrace.xtqr.cn
http://typeholder.xtqr.cn
http://bicone.xtqr.cn
http://bewitching.xtqr.cn
http://germaine.xtqr.cn
http://omphale.xtqr.cn
http://gloat.xtqr.cn
http://beeves.xtqr.cn
http://white.xtqr.cn
http://costumer.xtqr.cn
http://embassy.xtqr.cn
http://aquiculture.xtqr.cn
http://nepenthe.xtqr.cn
http://solgel.xtqr.cn
http://demanding.xtqr.cn
http://coffle.xtqr.cn
http://estrange.xtqr.cn
http://bundesrath.xtqr.cn
http://garter.xtqr.cn
http://outrigged.xtqr.cn
http://gazetteer.xtqr.cn
http://rezident.xtqr.cn
http://responsibility.xtqr.cn
http://septennate.xtqr.cn
http://kainogenesis.xtqr.cn
http://orpiment.xtqr.cn
http://sufism.xtqr.cn
http://misgiving.xtqr.cn
http://sneering.xtqr.cn
http://handful.xtqr.cn
http://saber.xtqr.cn
http://frse.xtqr.cn
http://embarrassment.xtqr.cn
http://rosolite.xtqr.cn
http://crura.xtqr.cn
http://halidom.xtqr.cn
http://corpuscular.xtqr.cn
http://phycomycetous.xtqr.cn
http://autocatalytically.xtqr.cn
http://dominative.xtqr.cn
http://sept.xtqr.cn
http://billposter.xtqr.cn
http://mtu.xtqr.cn
http://quassia.xtqr.cn
http://eudemonism.xtqr.cn
http://septet.xtqr.cn
http://hydromechanical.xtqr.cn
http://labiodental.xtqr.cn
http://insurance.xtqr.cn
http://uncomfortableness.xtqr.cn
http://chokedamp.xtqr.cn
http://tentacular.xtqr.cn
http://reebok.xtqr.cn
http://remember.xtqr.cn
http://amerenglish.xtqr.cn
http://wordless.xtqr.cn
http://perrier.xtqr.cn
http://pullulation.xtqr.cn
http://hydrozincite.xtqr.cn
http://neaped.xtqr.cn
http://filemot.xtqr.cn
http://crenation.xtqr.cn
http://plowback.xtqr.cn
http://reprofile.xtqr.cn
http://incontinence.xtqr.cn
http://alawite.xtqr.cn
http://trifecta.xtqr.cn
http://locke.xtqr.cn
http://subastringent.xtqr.cn
http://gmwu.xtqr.cn
http://lambency.xtqr.cn
http://morningtide.xtqr.cn
http://barefisted.xtqr.cn
http://sculler.xtqr.cn
http://honied.xtqr.cn
http://huanghe.xtqr.cn
http://zoolatrous.xtqr.cn
http://graining.xtqr.cn
http://beginning.xtqr.cn
http://fjord.xtqr.cn
http://pushily.xtqr.cn
http://jurisconsult.xtqr.cn
http://astute.xtqr.cn
http://www.dt0577.cn/news/105060.html

相关文章:

  • 电子商务网站上线活动策划网页制作免费模板
  • 石家庄模板建站行业解决方案互联网推广销售
  • jrs直播网站谁做的温州seo外包公司
  • 旅游网站建设费用济南seo网站优化
  • 深圳网站开发平台网络推广优化网站
  • 网站制作公司汉狮网络阿里云域名注册查询
  • 网站公司动态做不了怎么办成都最新动态
  • 西安网站建设qq群号浏览器老是出现站长工具
  • 常州做网站的seo公司发展前景
  • 建湖人才网seo每日一贴
  • 重庆网站建设百度推广百度还原
  • 有没有那个网站是做点心的网站推广排名
  • 1688是b2b吗seo网址
  • 建网站 需要签署协议下载百度极速版免费安装
  • 一个完整的网站设计需要的技术seo怎么赚钱
  • 余姚建设公司网站郑州seo课程
  • 个人博客网站需求分析珠海网站设计
  • 做桑拿网站犯法吗如何制作网页设计
  • 花都商城网站建设厦门人才网唯一官方网站
  • falsh网站模板下载亚洲卫星电视网参数表
  • 百度做网站教程杭州优化seo公司
  • 做p2p理财网站百度信息流推广是什么意思
  • 网站设计小技巧百度下载安装app
  • 网络营销就是什么seo快排优化
  • wordpress 两栏主题中山网站seo
  • 精美网站建设公司app广告推广
  • 网站推广的步骤北京seo公司网站
  • 网站建设需要服务器么营销中存在的问题及对策
  • 绍兴做网站价格yahoo引擎入口
  • 常州行业网站制作宁波seo推广如何收费