JUC介绍--阻塞队列(4组API)

6 篇文章 0 订阅
1 篇文章 0 订阅

常见队列家族

在这里插入图片描述

在这里插入图片描述
使用阻塞队列的情况:多线程并发处理,线程池
阻塞队列的四种API:

方式抛出异常有返回值,不抛出异常阻塞 等待超时等待
添加add()offer()put()offer(,)
移除remove()poll()take()poll(,)
判断队列首elementpeek--
public class Test {
    public static void main(String[] args) throws InterruptedException {
        //test01();
//        test02();
//        test03();
        test04();
    }

    /*
    抛出异常
     */
    public static void test01(){
        //设置队列大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.add("a"));//true
        System.out.println(blockingQueue.add("b"));//true
        System.out.println(blockingQueue.add("c"));//true
        //队列满了再添加会抛出异常 IllegalStateException: Queue full
        //System.out.println(blockingQueue.add("d"));

        //查看队首元素是谁
        System.out.println(blockingQueue.element());

        System.out.println(blockingQueue.remove());//a
        System.out.println(blockingQueue.remove());//b
        //弹出两个后再查看队首元素是谁
        System.out.println(blockingQueue.element());
        System.out.println(blockingQueue.remove());//c
        //队列空了再取就会抛出异常 NoSuchElementException
        //System.out.println(blockingQueue.remove());
    }
    /*
    有返回值,不抛出异常
     */
    public static void test02(){
        //设置队列大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("a"));//true
        System.out.println(blockingQueue.offer("b"));//true
        System.out.println(blockingQueue.offer("c"));//true

        //查看队首元素是谁
        System.out.println(blockingQueue.peek());

        //队列满了再添加会返回false,不会抛出异常
        System.out.println(blockingQueue.offer("d"));//false
        System.out.println(blockingQueue.poll());//a
        System.out.println(blockingQueue.poll());//b
        //弹出两个后再查看队首元素是谁
        System.out.println(blockingQueue.peek());
        System.out.println(blockingQueue.poll());//c
        //队列空了再取就会返回null,不会抛出异常
        System.out.println(blockingQueue.poll());//null
    }

    /*
    等待,阻塞(一直阻塞)
     */
    public static void test03() throws InterruptedException {
        //设置队列大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
//        blockingQueue.put("d");//队列没有位置,会一直阻塞

        System.out.println(blockingQueue.take());//a
        System.out.println(blockingQueue.take());//b
        System.out.println(blockingQueue.take());//c
//        System.out.println(blockingQueue.take());//队列空了,会一直阻塞
    }

        /*
    等待,阻塞(等待超时)
     */
        public static void test04() throws InterruptedException {
            //设置队列大小
            ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
            System.out.println(blockingQueue.offer("a"));//true
            System.out.println(blockingQueue.offer("b"));//true
            System.out.println(blockingQueue.offer("c"));//true
            //队列满了再添加先等2S,如果超过2S还没有位置就退出或执行下面的代码
            blockingQueue.offer("d",2, TimeUnit.SECONDS);

            //查看队首元素是谁
            System.out.println(blockingQueue.peek());

            System.out.println(blockingQueue.poll());//a
            System.out.println(blockingQueue.poll());//b
            System.out.println(blockingQueue.poll());//c
            //队列空了再取最多等2S,超过2S没有就退出或执行下面的代码
            blockingQueue.poll(2,TimeUnit.SECONDS);
            System.out.println("==============");
        }

}

SynchronousQueue同步队列
和其他BlockingQueue不一样,SynchronousQueue不存储元素
put一个元素,必须从里面先take出来,否则不能put进去下一个
可以理解为队列长度为1的特殊BlockingQueue

public class SynchronousQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new SynchronousQueue<>();
        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"put 1");
                blockingQueue.put("1");
                System.out.println(Thread.currentThread().getName()+"put 2");
                blockingQueue.put("2");
                System.out.println(Thread.currentThread().getName()+"put 3");
                blockingQueue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"T1").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"T2").start();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值