Java并发BlockingQueue接口

本文介绍了Java中的BlockingQueue接口,它是Queue接口的扩展,支持线程安全的元素添加和检索。主要讨论了其关键方法如offer(),poll(),take()以及如何在多线程环境下如生产者-消费者模型中使用ArrayBlockingQueue进行操作。
摘要由CSDN通过智能技术生成

java.util.concurrent.BlockingQueue接口是Queue接口的子接口,另外还支持诸如在检索元素之前等待队列变为非空的操作,并在存储元素之前等待队列中的空间变得可用 。

BlockingQueue接口中的方法

序号方法描述
1boolean add(E e)将指定的元素插入到此队列中,如果可以立即执行此操作,而不会违反容量限制,在成功时返回true,并且如果当前没有空间可用,则抛出IllegalStateException
2boolean contains(Object o)如果此队列包含指定的元素,则返回true
3int drainTo(Collection<? super E> c)从该队列中删除所有可用的元素,并将它们添加到给定的集合中。
4int drainTo(Collection<? super E> c, int maxElements)最多从该队列中删除给定数量的可用元素,并将它们添加到给定的集合中。
5boolean offer(E e)将指定的元素插入到此队列中,如果可以立即执行此操作而不违反容量限制,则成功返回true,如果当前没有空间可用,则返回false
6boolean offer(E e, long timeout, TimeUnit unit)将指定的元素插入到此队列中,等待指定的等待时间(如有必要)才能使空间变得可用。
7E poll(long timeout, TimeUnit unit)检索并删除此队列的头,等待指定的等待时间(如有必要)使元素变为可用。
8void put(E e)将指定的元素插入到此队列中,等待空间/容量可用。
9int remainingCapacity()返回此队列可理想地(在没有内存或资源约束的情况下)接受而不阻止的附加元素数,如果没有内在限制则返回Integer.MAX_VALUE
10boolean remove(Object o)从该队列中删除指定元素的单个实例(如果存在)。
11E take()检索并删除此队列的头,如有必要,等待元素可用。

实例

以下TestThread程序显示了基于线程的环境中BlockingQueue接口的使用。

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class TestThread {

   public static void main(final String[] arguments) throws InterruptedException {
      BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10);

      Producer producer = new Producer(queue);
      Consumer consumer = new Consumer(queue);

      new Thread(producer).start();
      new Thread(consumer).start();

      Thread.sleep(4000);
   }  


   static class Producer implements Runnable {

      private BlockingQueue<Integer> queue;

      public Producer(BlockingQueue queue){
         this.queue = queue;
      }

      @Override
      public void run() {
         Random random = new Random();

         try {
            int result = random.nextInt(100);
            Thread.sleep(1000);
            queue.put(result);
            System.out.println("Added: " + result);
            result = random.nextInt(100);
            Thread.sleep(1000);
            queue.put(result);
            System.out.println("Added: " + result);
            result = random.nextInt(100);
            Thread.sleep(1000);
            queue.put(result);
            System.out.println("Added: " + result);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }       
   }

   static class Consumer implements Runnable {

      private BlockingQueue<Integer> queue;

      public Consumer(BlockingQueue queue){
         this.queue = queue;
      }

      @Override
      public void run() {
         try {
            System.out.println("Removed: " + queue.take());
            System.out.println("Removed: " + queue.take());
            System.out.println("Removed: " + queue.take());
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }
}

Java

这将产生以下结果 -

Added: 73
Removed: 73
Added: 19
Removed: 19
Added: 47
Removed: 47

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

智慧浩海

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

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

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

打赏作者

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

抵扣说明:

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

余额充值