并发队列 ConcurrentLinkedQueue 及 BlockingQueue 接口实现的四种队列

本文介绍了Java并发编程中两种类型的队列:基于无锁的ConcurrentLinkedQueue和阻塞队列BlockingQueue,包括ArrayBlockingQueue、LinkedBlockingQueue、PriorityBlockingQueue和DelayQueue。ConcurrentLinkedQueue适用于高并发场景,而BlockingQueue接口提供了多种实现,如ArrayBlockingQueue适合定长队列,LinkedBlockingQueue支持高效并发,PriorityBlockingQueue基于优先级,DelayQueue则支持延迟取出元素。SynchronousQueue则是一种没有容量的队列,用于快速传递元素。
摘要由CSDN通过智能技术生成

队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。

在队列这种数据结构中,最先插入的元素将是最先被删除的元素;反之最后插入的元素将是最后被删除的元素,因此队列又称为“先进先出”(FIFO—first in first out)的线性表。

在并发队列上JDK提供了两套实现,一个是以ConcurrentLinkedQueue为代表的高性能队列,一个是以BlockingQueue接口为代表的阻塞队列,无论哪种都继承自Queue。


一、ConcurrentLinkedQueue

是一个适用于高并发场景下的队列,通过无锁的方式,实现了高并发状态下的高性能,通常ConcurrentLikedQueue性能好于BlockingQueue。

它是一个基于连接节点的无界线程安全队列。该队列的元素遵循先进先出的原则。头是最先加入的,尾是最近加入的,该队列不允许null元素。

ConcurrentLinkedQueue重要方法:

add()和offer()都是加入元素的方法(在ConcurrentLinkedQueue中,这两个方法没有任何区别)。

poll()和peek()都是取头元素节点,区别在于前者会删除元素,后者不会。

下面看一个例子:

  /* 
   * 一个基于链接节点的、无界的、线程安全的队列。此队列按照 FIFO(先进先出)原则对元素进行排序。队列的头部 是队列中时间最长的元素。队列的尾部 
   * 是队列中时间最短的元素。新的元素插入到队列的尾部,队列检索操作从队列头部获得元素。当许多线程共享访问一个公共 collection 
   * 时,ConcurrentLinkedQueue 是一个恰当的选择。此队列不允许 null 元素。 
   */    
  private void concurrentLinkedQueueTest() {    
      ConcurrentLinkedQueue<String> concurrentLinkedQueue = new ConcurrentLinkedQueue<String>();    
      concurrentLinkedQueue.add("a");    
      concurrentLinkedQueue.add("b");    
      concurrentLinkedQueue.add("c");    
      concurrentLinkedQueue.offer("d"); // 将指定元素插入到此队列的尾部。    
      concurrentLinkedQueue.peek(); // 检索并移除此队列的头,如果此队列为空,则返回 null。    
      concurrentLinkedQueue.poll(); // 检索并移除此队列的头,如果此队列为空,则返回 null。    
  
      for (String str : concurrentLinkedQueue) {    
          System.out.println(str);    
      }    
  }  

注意:ConcurrentLinkedQueue的API.size() 是要遍历一遍集合的,速很慢,所以判空时,尽量要避免用size(),而改用isEmpty()。

二、BlockingQueue接口

ArrayBlockingQueue:基于数组的阻塞队列实现,在ArrayBlockingQueue内部,维护了一个定长数组,以便缓存队列中的数据对象,其内部没实现读写分离,也就意味着生产和消费不能完全并行,长度是需要定义的,可以指定先进先出或者先进后出,也叫有界队列,在很多场合非常适合使用。

package concurrent;  
  
import java.util.concurrent.ArrayBlockingQueue;  
import java.util.concurrent.TimeUnit;  
  
public class ArrayBlockingQueueTest {  
  
    private ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(10);  
  
    public static void main(String[] args) throws InterruptedException {  
        final ArrayBlockingQueueTest arrayBlockingQueueTest = new ArrayBlockingQueueTest();  
        new Thread(new Runnable() {  
            public void run() {  
                try {  
                    arrayBlockingQueueTest.producer();  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        }).start();  
        new Thread(new Runnable() {  
            public void run() {  
                try {  
                    arrayBlockingQueueTest.consumer();  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        }).start();  
  
  
    }  
  
    private void producer() throws InterruptedException {  
        for(int i=0; i<100; i++) {  
            System.out.println("arrayBlockingQueue.size()="+arrayBlockingQueue.size());  
            //Thread.sleep(1000);  
            //队列满了之后会直接抛出异常  
            //arrayBlockingQueue.add(i);  
            //队列满了之后会等待队列腾出空间  
            //arrayBlockingQueue.put(i);  
            //将指定的元素插入到此队列的尾部(如果立即可行且不会超过该队列的容量),在成功时返回 true,如果此队列已满,则返回 false。  
            arrayBlockingQueue.offer(i);  
  
        }  
    }  
  
    private void consumer() throws InterruptedException {  
        w
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二一点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值