BlockingQueue四组API

阻塞队列

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mOzXsHGu-1654973647624)(C:\Users\38492\AppData\Local\Temp\1654970501452.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xd30NitP-1654973647634)(C:\Users\38492\AppData\Local\Temp\1654970625596.png)]

什么情况下我们会使用 阻塞队列?

多线程并发处理,线程池!

一定要学会使用队列

添加移除

BlockingQueue(阻塞队列)四组API

方式抛出异常有返回值,不抛出异常阻塞等待超时等待
添加addofferputoffer
移除removepolltakepoll
检测队首元素elementpeek--

第一组:(抛出异常)

package bq;

import java.util.concurrent.ArrayBlockingQueue;

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.element());//查看队首是谁 
        
        //队列为3,我们添加第四个元素 : IllegalStateException 抛出异常!(非法状态异常)
        //System.out.println(blockingQueue.add("d"));
        System.out.println("----------------");
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());

        //NoSuchElementException 抛出异常!(没有这样的元素异常)
        //System.out.println(blockingQueue.remove());
    }
}

第二组(有返回值,不抛出异常)

package bq;

import java.util.concurrent.ArrayBlockingQueue;

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.peek());//查看队首是谁
         
        //false 不抛出异常!
        //System.out.println(blockingQueue.offer("d"));

        System.out.println("----------------");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        //null 不抛出异常!
        System.out.println(blockingQueue.poll());
    }
}

第三组(阻塞等待)

package bq;

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

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"); // 队列没有位置了,一直阻塞

        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        //System.out.println(blockingQueue.take()); // 没有这个元素,一直阻塞
    }
}

第四组(超时等待)

package bq;

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

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秒就退出

        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));// 等待超过2秒就退出
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

已转行@

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

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

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

打赏作者

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

抵扣说明:

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

余额充值