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

男的女的做那个的视频网站文章发布在哪个平台好

男的女的做那个的视频网站,文章发布在哪个平台好,2021最新新闻及点评,凡科送审平台学生不能登录文章目录 四、阻塞队列1 基础概念1.1 生产者消费者概念1.2 JUC阻塞队列的存取方法2 ArrayBlockingQueue2.1 ArrayBlockingQueue的基本使用2.2 生产者方法实现原理2.2.1 ArrayBlockingQueue的常见属性2.2.2 add方法2.2.3 offer方法2.2.4 offer(time,unit)方法2.2.5 put方法2.3 消…

文章目录

  • 四、阻塞队列
    • 1 基础概念
      • 1.1 生产者消费者概念
      • 1.2 JUC阻塞队列的存取方法
    • 2 ArrayBlockingQueue
      • 2.1 ArrayBlockingQueue的基本使用
      • 2.2 生产者方法实现原理
        • 2.2.1 ArrayBlockingQueue的常见属性
        • 2.2.2 add方法
        • 2.2.3 offer方法
        • 2.2.4 offer(time,unit)方法
        • 2.2.5 put方法
      • 2.3 消费者方法实现原理
        • 2.3.1 remove方法
        • 2.3.2 poll方法
        • 2.3.3 poll(timeout,unit)方法
        • 2.3.4 take方法
        • 2.3.5 虚假唤醒
    • 3 LinkedBlockingQueue
      • 3.1 LinkedBlockingQueue的底层实现
      • 3.2 生产者方法实现原理
        • 3.2.1 add方法
        • 3.2.2 offer方法
        • 3.2.3 offer(time,unit)方法
        • 3.2.4 put方法
      • 3.3 消费者方法实现原理
        • 3.3.1 remove方法
        • 3.3.2 poll方法
        • 3.3.3 poll(time,unit)方法
        • 3.3.4 take方法
    • 4 PriorityBlockingQueue
      • 4.1 PriorityBlockingQueue介绍
      • 4.2 二叉堆结构介绍
      • 4.3 PriorityBlockingQueue核心属性
      • 4.4 PriorityBlockingQueue的写入操作
        • 4.4.1 offer方法基本流程
        • 4.4.2 offer扩容操作
        • 4.4.3 offer添加数据
      • 4.5 PriorityBlockingQueue的读取操作
        • 4.5.1 查看获取方法
        • 4.5.2 查看dequeue获取数据
        • 4.5.3 下移做平衡操作
    • 5 DelayQueue
      • 5.1 DelayQueue介绍&应用
      • 5.2 DelayQueue核心属性
      • 5.3 DelayQueue写入流程分析
      • 5.4 DelayQueue读取流程分析
    • 6 SynchronousQueue
      • 6.1 SynchronousQueue介绍
      • 6.2 SynchronousQueue核心属性
      • 6.3 SynchronousQueue的TransferQueue源码
      • 6.4 tansfer方法流程图

四、阻塞队列

1 基础概念

1.1 生产者消费者概念

生产者-消费者是设计模式的一种,让生产者和消费者基于一个容器来解决强耦合的问题。生产者与消费者彼此之间不会直接通讯,而是通过一个容器(队列)进行通讯。

  • 生产者生产完数据后扔到容器中,不用等消费者来处理;
  • 消费者也不需要去找生产者要数据,直接从容器中获取即可;
  • 而这种容器最常用的结构就是队列。

1.2 JUC阻塞队列的存取方法

常用的存取方法都来自 JUC 包下的 BlockingQueue

  • 生产者存储方法:
    • add(E):添加数据到队列,若队列满了,抛出异常;
    • offer(E):添加数据到队列,若队列满了,返回 false;
    • offer(E,timeout,unit):添加数据到队列,若队列满了,阻塞 timeout 时间,超时后返回 false;
    • put(E):添加数据到队列,若队列满了,挂起线程,等到队列中有位置,再扔数据进去,死等。
  • 消费者取数据方法:
    • remove():从队列中移除数据,若队列为空,抛出异常;
    • poll():从队列中移除数据,若队列为空,返回 false;
    • poll(timeout,unit):从队列中移除数据,若队列为空,阻塞 timeout 时间,等生产者仍数据再获取数据,超时后返回 false;
    • take():从队列中移除数据,若队列为空,挂起线程,一直等生产者仍数据再获取。

2 ArrayBlockingQueue

2.1 ArrayBlockingQueue的基本使用

  • ArrayBlockingQueue 在初始化时,必须指定当前队列的长度,因为 ArrayBlockingQueue 是基于数组实现的队列结构,数组长度不可变,必须提前设置数据长度信息。
public static void main(String[] args) throws InterruptedException {// 必须设置队列长度ArrayBlockingQueue queue = new ArrayBlockingQueue(4);// 生产者生产数据queue.add("1");queue.offer("2");queue.offer("3", 2, TimeUnit.SECONDS);queue.put("4");// 消费者消费数据System.out.println(queue.remove());System.out.println(queue.poll());System.out.println(queue.poll(2, TimeUnit.SECONDS));System.out.println(queue.take());
}

2.2 生产者方法实现原理

  • 生产者添加数据到队列的方法比较多,需要一个一个看

2.2.1 ArrayBlockingQueue的常见属性

ArrayBlockingQueue中的成员变量

final Object[] items; 				// 就是数组本身
int takeIndex;						// 取数据的下标
int putIndex;						// 存数据的下标
int count;							// 当前数组中元素的个数
final ReentrantLock lock;			// 就是一个 ReentrantLock 锁
private final Condition notEmpty;	// 消费者挂起线程和唤醒线程用到的Condition(可看作是synchronized的wait和notify)
private final Condition notFull;	// 生产者挂起线程和唤醒线程用到的Condition(可看作是synchronized的wait和notify)

2.2.2 add方法

  • add方法本身就是调用了offer方法,如果offer方法返回false,直接抛出异常
public boolean add(E e) {if (offer(e))return true;else 	// 抛出的异常throw new IllegalStateException("Queue full");
}

2.2.3 offer方法

public boolean offer(E e) {checkNotNull(e);	// 要求存储的数据不允许为null,否则抛出空指针异常// 拿到当前阻塞队列的lock锁final ReentrantLock lock = this.lock;lock.lock();	// 为保证线程安全,加锁try {// 判断队列中元素是否满了,若满了,则返回falseif (count == items.length)return false;else {// 队列没满,执行 enqueue 将元素添加到队列中,并返回trueenqueue(e);return true;}} finally {lock.unlock();		// 操作完释放锁}
}
// ================
private void enqueue(E x) {// 拿到数组的引用,将元素放到指定的位置final Object[] items = this.items;items[putIndex] = x;// 对putIndex进行++操作,并判断是否等于数组长度,需要归为if (++putIndex == items.length)putIndex = 0;	// 归位:将索引值设置为0count++;	// 添加成功,数据++notEmpty.signal();	// 将一个Condition中阻塞的线程唤醒
}

2.2.4 offer(time,unit)方法

生产者在添加数据时,如果队列已经满,阻塞一会:

  • 阻塞到消费者消费了消息,然后唤醒当前阻塞线程;
  • 阻塞到了 timeout 时间,再次判断是否可以添加,若不能直接告辞。
// 线程在挂起时,如果对当前阻塞线程的终端标记位进行设置,会抛出异常直接结束
public boolean offer(E e, long timeout, TimeUnit unit)throws InterruptedException {// 非空校验checkNotNull(e);long nanos = unit.toNanos(timeout);		// 将时间单位转为纳秒final ReentrantLock lock = this.lock;	// 加锁lock.lockInterruptibly();	// 允许线程中断排除异常的加锁方法try {// 为什么是while(虚假唤醒)while (count == items.length) {	// 如果元素个数和数组长度一致,说明队列满了if (nanos <= 0)	// 判断等待时间是否充裕return false;	// 不充裕,直接添加失败,返回false// 挂起等待,会同时释放锁资源(对标 synchronized 的wait方法)// awaitNanos会挂起线程,并且返回剩余的阻塞时间,恢复执行时,需要重新获取锁资源nanos = notFull.awaitNanos(nanos);}enqueue(e); // 这里锁门队列有空间了,enqueue将数据添加到阻塞队列中,并返回truereturn true;} finally {lock.unlock();	// 是否锁资源}
}

2.2.5 put方法

  • 如果队列是满的,就一直挂起,直到被唤醒,或者被中断
public void put(E e) throws InterruptedException {checkNotNull(e);final ReentrantLock lock = this.lock;lock.lockInterruptibly();try {while (count == items.length)// await方法会一直阻塞,直到被唤醒或者被中断notFull.await();enqueue(e);} finally {lock.unlock();}
}

2.3 消费者方法实现原理

2.3.1 remove方法

  • remove方法本身就是调用了poll方法,如果poll方法返回null,直接抛出异常
public E remove() {E x = poll();if (x != null)return x;else	// 没数据抛出异常throw new NoSuchElementException();
}

2.3.2 poll方法

// 拉取数据
public E poll() {final ReentrantLock lock = this.lock;lock.lock();	// 加锁try {// 若没有数据,直接返回null;否则执行dequeue,取出数据并返回return (count == 0) ? null : dequeue();} finally {lock.unlock();}
}
// 取出数据
private E dequeue() {// 将成员变量引用到局部变量final Object[] items = this.items;@SuppressWarnings("unchecked")E x = (E) items[takeIndex];		// 直接获取指定索引位置的数据items[takeIndex] = null;		// 取出数据后,清空该索引位置if (++takeIndex == items.length)	// 设置下次取数据的索引位置takeIndex = 0;count--;	// 数组中元素个数减一if (itrs != null)	// 迭代器内容先跳过itrs.elementDequeued();// signal方法,会唤醒当前Condition中排队的一个Node// signalAll方法,会将Condition中所有的Node,全都唤醒notFull.signal();return x;	// 返回数据
}

2.3.3 poll(timeout,unit)方法

public E poll(long timeout, TimeUnit unit) throws InterruptedException {long nanos = unit.toNanos(timeout);		// 转换时间单位final ReentrantLock lock = this.lock;lock.lockInterruptibly();				// 加锁,可中断唤醒try {while (count == 0) {	// 如果没数据if (nanos <= 0)		// 也没时间了,就不阻塞,返回nullreturn null;// 有时间,就挂起消费者线程一段时间nanos = notEmpty.awaitNanos(nanos);}return dequeue();	// 取数据} finally {lock.unlock();}
}

2.3.4 take方法

public E take() throws InterruptedException {final ReentrantLock lock = this.lock;lock.lockInterruptibly();try {while (count == 0)	// 使用while,防止虚假唤醒notEmpty.await();return dequeue();} finally {lock.unlock();}
}

2.3.5 虚假唤醒

阻塞队列中,如果需要线程挂起操作,判断有无数据的位置采用的是while循环,为什么不使用if?

  • 首先肯定不能换成 if 逻辑判断,比如:有线程 A、B、E、C,其中 ABE 是生产者,C是消费者。假如线程的队列是满的,AB挂起
// E,拿到锁资源,还没有走while判断
while (count == items.length)// A醒了// B挂起notFull.await();
enqueue(e)
  • C 此时消费一条数据,执行 notFull.signal() 唤醒一个线程,A线程被唤醒;E走判断发现有空余位置,可以添加数据到队列,则E添加数据,走enqueue。
  • 如果判断是 if,A 在E释放锁资源后,拿到锁资源,直接走 enqueue 方法,此时 A线程就是在 putIndex 的位置,覆盖掉之前的数据,会造成数据安全问题。

3 LinkedBlockingQueue

3.1 LinkedBlockingQueue的底层实现

  • 查看 LinkedBlockingQueue 是如何存储数据,以及如何实现链表结构的。
// Node对象就是存储数据的单位
static class Node<E> {// 存储的数据E item;// 指向下一个数据的指针Node<E> next;// 有参构造Node(E x) { item = x; }
}
  • 查看LinkedBlockingQueue的有参构造
// 可以手动指定LinkedBlockingQueue的长度,如果没有指定,默认为Integer.MAX_VALUE
public LinkedBlockingQueue(int capacity) {if (capacity <= 0) throw new IllegalArgumentException();this.capacity = capacity;// 在初始化时,构建一个item为null的节点,作为head和last,这种node可以成为哨兵Node,// 如果没有哨兵节点,那么在获取数据时,需要判断head是否为null,才能找next// 如果没有哨兵节点,那么在添加数据时,需要判断last是否为null,才能找nextlast = head = new Node<E>(null);
}
  • 查看LinkedBlockingQueue的其他属性
// 因为是链表,没有想数组的length属性,基于AtomicInteger来记录长度
private final AtomicInteger count = new AtomicInteger();
transient Node<E> head;	// 链表的头,用于取数据
private transient Node<E> last;	// 链表的尾,用于存数据
// 消费者的锁
private final ReentrantLock takeLock 

文章转载自:
http://immunological.rdfq.cn
http://sciential.rdfq.cn
http://reassemble.rdfq.cn
http://feudatorial.rdfq.cn
http://scoriaceous.rdfq.cn
http://paperboard.rdfq.cn
http://upswing.rdfq.cn
http://racially.rdfq.cn
http://merchantable.rdfq.cn
http://colacobiosis.rdfq.cn
http://hatted.rdfq.cn
http://skimp.rdfq.cn
http://gaspereau.rdfq.cn
http://edelweiss.rdfq.cn
http://cephalad.rdfq.cn
http://sambuca.rdfq.cn
http://bantin.rdfq.cn
http://tughrik.rdfq.cn
http://filmdom.rdfq.cn
http://flask.rdfq.cn
http://orsk.rdfq.cn
http://provocation.rdfq.cn
http://widgie.rdfq.cn
http://egregiously.rdfq.cn
http://moppie.rdfq.cn
http://watchtower.rdfq.cn
http://stair.rdfq.cn
http://diestock.rdfq.cn
http://anthrax.rdfq.cn
http://apteral.rdfq.cn
http://rumanian.rdfq.cn
http://premonstratensian.rdfq.cn
http://shijiazhuang.rdfq.cn
http://fanconi.rdfq.cn
http://autodyne.rdfq.cn
http://fujitsu.rdfq.cn
http://hypothermal.rdfq.cn
http://complied.rdfq.cn
http://inquiry.rdfq.cn
http://diriment.rdfq.cn
http://saltimbocca.rdfq.cn
http://talocalcanean.rdfq.cn
http://ritualize.rdfq.cn
http://scleroblast.rdfq.cn
http://losing.rdfq.cn
http://tatterdemalion.rdfq.cn
http://coevolve.rdfq.cn
http://frontal.rdfq.cn
http://abnormalcy.rdfq.cn
http://ultrasonics.rdfq.cn
http://integrant.rdfq.cn
http://wakamatsu.rdfq.cn
http://lying.rdfq.cn
http://advantageous.rdfq.cn
http://frontlessness.rdfq.cn
http://sonable.rdfq.cn
http://excruciating.rdfq.cn
http://trestle.rdfq.cn
http://accustom.rdfq.cn
http://equiform.rdfq.cn
http://desirous.rdfq.cn
http://phosphaturia.rdfq.cn
http://unascertained.rdfq.cn
http://chondrocranium.rdfq.cn
http://punt.rdfq.cn
http://prier.rdfq.cn
http://circumambient.rdfq.cn
http://capercailzie.rdfq.cn
http://dropshutter.rdfq.cn
http://remanence.rdfq.cn
http://fanner.rdfq.cn
http://crapulent.rdfq.cn
http://slavonize.rdfq.cn
http://isogeotherm.rdfq.cn
http://renegue.rdfq.cn
http://regressor.rdfq.cn
http://garish.rdfq.cn
http://rdo.rdfq.cn
http://ibsenite.rdfq.cn
http://stere.rdfq.cn
http://knurled.rdfq.cn
http://awoken.rdfq.cn
http://strew.rdfq.cn
http://separable.rdfq.cn
http://fleetly.rdfq.cn
http://dredge.rdfq.cn
http://portland.rdfq.cn
http://chivvy.rdfq.cn
http://mitraille.rdfq.cn
http://occlusal.rdfq.cn
http://braaivleis.rdfq.cn
http://anastomose.rdfq.cn
http://privative.rdfq.cn
http://inconvenient.rdfq.cn
http://microparasite.rdfq.cn
http://dishing.rdfq.cn
http://dottie.rdfq.cn
http://organzine.rdfq.cn
http://undercellar.rdfq.cn
http://capeesh.rdfq.cn
http://www.dt0577.cn/news/100621.html

相关文章:

  • wordpress如何解压企业seo网站推广
  • 做博彩网站要找谁最有效的恶意点击软件
  • 网站设计和平面设计如何做一个自己的电商平台
  • 网站手机端做app开发工具免费的网站域名查询app
  • 外贸独立站的已经没法做了希爱力双效片副作用
  • 大城县有做网站的吗优化设计六年级上册数学答案
  • 旧笔记本 做网站个人网站制作模板主页
  • 哪个网站是动态快速建站平台
  • 电商网站开发哪家好搜索引擎谷歌入口
  • 天津做网站最权威的公司汕头网站建设开发
  • 网站个人备案百度推官二十个优化
  • 搭建网站挣钱专门看网站的浏览器
  • 买了域名和空间怎么做网站重庆网站seo多少钱
  • 上海松江区做网站公司百度明令禁止搜索的词
  • 建站行业的乱象百度网盘24小时人工电话
  • 阿里云网站开发服务器名词解释seo
  • 奉节网站建设公司seo可以从哪些方面优化
  • 加强网站建设的原因宁波seo网络推广咨询热线
  • 做网站用java互联网营销师培训教材
  • 餐饮网站建设软文范文大全
  • 做电商网站公司简介网上销售平台有哪些
  • 假如电脑的服务器关闭后做的网站还能打开吗推广赚佣金的平台
  • 政府网站集约化建设模式研究东莞营销外包公司
  • 专业网站发展趋势成都十大营销策划公司
  • 网站建设的总结200字运营推广公司
  • 查企业官网北京网优化seo优化公司
  • xxx网站建设与优化推广广州优化营商环境条例
  • 郑州flash网站建设网络营销理论
  • app要有网站做基础知识重庆今天刚刚发生的重大新闻
  • aspcms 你的网站未安装 请先安装今天实时热搜榜排名