BlockingQueue是并发包Concurrent下的阻塞队列,用于多线程。

核心方法:

放入数据(都不可以加入null):

1,boolean add(E e);  

//队列已满时,返回illlgalStateException;

2,boolean offer(E e); 

//队列已满时,返回false;不可以加入null.有容量限制时优先选择这个方法。

3,boolean offer(E e,long timeout,TimeUnit unit) throws InterruptedException;

//如果没有空间加入,会等待timeout时间,在这段时间内有空间就可以加入,这段时间过后还没加入,便返回false.

4,void put(E e) throws InterruptedException;

//没有空间加入,会一直阻塞直到有空间加入。

取出数据:

1,E take();   

//阻塞直到队列不为空,移除并返回队列的头。

2,E poll(long timeout,TimeUnit unit);  

//队列不为空返回队列的头;队列为空可以等待timeout时间,队列不为空时返回队列的头,取不到返回null.

3,int drainTo(Collection<? super E> c);

//把队列里所有数据移到c中

已经了解了BlockingQueue的核心方法以下是实现了BlockingQueue的类:

阻塞队列BlockingQueue

阻塞队列BlockingQueue

1, ArrayBlockingQueue(基于数组)

2, DelayQueue(基于优先级队列)

3, LinkedBlockingQueue(基于链表)

4, PriorityBlockingQueue(基于优先级队列)

5, SynchronousQueue(同步队列)

下一篇详细介绍各个阻塞队列的具体实现。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2021-05-13
  • 2021-06-01
猜你喜欢
  • 2021-05-27
  • 2021-09-18
  • 2021-10-24
  • 2022-12-23
相关资源
相似解决方案