0
点赞
收藏
分享

微信扫一扫

Java - BlockingQueue学习

BlockingQueue是阻塞队列,继承Queue,在Queue的基础上添加了阻塞接口,实现阻塞功能。

  • BlockingQueue类图


  • BlockingQueue接口方法


其中offer(e, timeout, unit)poll(timeout, unit)为超时方法,puttake为阻塞方法,

抛异常 不抛异常 超时 阻塞
队尾添加 add offer offer(e, timeout, unit) put
队首删除获取 remove take poll(timeout, unit) poll
队首获取 element peek

BlockingQueue具体实现


以上几种BlockingQueue的实现均在线程池中有所体现

  1. ArrayBlockingQueue - 数组阻塞队列,底层容器是循环数组
  2. LinkedBlockingQueue - 链表阻塞队列,底层容器是链表
  3. SynchronousQueue - 同步队列,特性为双端阻塞,可以作为通信机制
  4. PriorityBlockingQueue - 优先队列,底层容器使用的是堆,用来实现优先队列
  5. DelayedWorkQueue - 延迟队列,底层容器使用的是堆,按照延迟时间顺序出列,并按照延迟时间准时出列。
举报

相关推荐

0 条评论