Java并发编程——阻塞队列(BlockingQueue)

阻塞队列(BlockingQueue)是一个支持两个附加操作的队列。这两个附加的操作是:在队列为空时,获取元素的线程会等待队列变为非空。当队列满时,存储元素的线程会等待队列可用。阻塞队列常用于生产者和消费者的场景,生产者是往队列里添加元素的线程,消费者是从队列里拿元素的线程。阻塞队列就是生产者存放元素的容器,而消费者也只从容器里拿元素。
以下所有测试使用的队列长度为3

一、抛出异常

1.添加:add()

使用add()方法向队列中添加元素,若多添加,会报java.lang.IllegalStateException: Queue full异常。

public class Test {
    public static void main(String[] args) {
        test1();
    }
    //抛出异常
    public static void test1() {
        ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        System.out.println(blockingQueue.add("d"));
    }
}

在这里插入图片描述

2.移除:remove()

使用remove()方法向队列中移除元素,若多移除,会报java.util.NoSuchElementException异常。

public class Test {
    public static void main(String[] args) {
        test1();
    }
    //抛出异常
    public static void test1() {
        ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        System.out.println("===============");
        blockingQueue.remove();
        blockingQueue.remove();
        blockingQueue.remove();
        blockingQueue.remove();
    }
}

在这里插入图片描述

二、有返回值,不抛出异常

1.添加:offer()

使用offer()方法向队列中添加元素,若多添加,会返回false。

public class Test {
    public static void main(String[] args) {
        test2();
    }

    //有返回值,不抛出异常
    public static void test2() {
        ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d"));
    }
}

在这里插入图片描述

2.移除:poll()

使用poll()方法向队列中移除元素,若多移除会返回null

public class Test {
    public static void main(String[] args) {
        test2();
    }

    //有返回值,不抛出异常
    public static void test2() {
        ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println("===============");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
    }
}

在这里插入图片描述

三、阻塞等待

1.添加:put()

使用put()方法向队列中添加元素,若多添加,就会阻塞,一直等待下去。

public class Test {
    public static void main(String[] args) throws InterruptedException {
        test3();
    }
    //阻塞等待
    public static void test3() throws InterruptedException {
        ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        blockingQueue.put("d");
    }
}

在这里插入图片描述

2.移除:take()

使用take()方法向队列中移除元素,若多移除会一直等待。

public class Test {
    public static void main(String[] args) throws InterruptedException {
        test3();
    }
    //阻塞等待
    public static void test3() throws InterruptedException {
        ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        System.out.println("===============");
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
    }
}

四、超时等待

1.添加:offer(E e, long timeout, TimeUnit unit)

使用offer()方法向队列中添加元素,设置超时时间,若多添加且超过时间,会返回false。

public class Test {
    public static void main(String[] args) throws InterruptedException {
        test4();
    }

    //超时等待
    public static void test4() throws InterruptedException {
        ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d",2, TimeUnit.SECONDS));
    }
}

在这里插入图片描述

2.移除:poll()

使用poll()方法向队列中移除元素,设置超时时间,若多移除且超过时间会返回null

public class Test {
    public static void main(String[] args) throws InterruptedException {
        test4();
    }

    //超时等待
    public static void test4() throws InterruptedException {
        ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println("===============");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS));
    }
}

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Alchemy_Ding

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

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

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

打赏作者

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

抵扣说明:

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

余额充值