十二、BlockingQueue阻塞队列的使用

BlockingQueue阻塞队列的使用

  1. BlockingQueue是阻塞队列。它是一个接口,主要的实现类有

在这里插入图片描述

  1. add方法,队列添加满时抛出异常

    public class BlockingQueueTest {
        public static void main(String[] args) {
            test1();
        }
        
        //抛出异常的方法
        public static void test1(){
            BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
            System.out.println(blockingQueue.add(1));
            System.out.println(blockingQueue.add(2));
            System.out.println(blockingQueue.add(3));
    
            System.out.println(blockingQueue.add(4));   //抛出异常
        }
    }
    

    添加第4个时 抛出异常

在这里插入图片描述

  1. remove方法,队列为空时,继续移除元素抛出异常

    public class BlockingQueueTest {
        public static void main(String[] args) {
            test1();
        }
        
        //抛出异常的方法
        public static void test1(){
            BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
            System.out.println(blockingQueue.add(1));
            System.out.println(blockingQueue.add(2));
            System.out.println(blockingQueue.add(3));
    
            System.out.println("=====================================");
    
            System.out.println(blockingQueue.remove());
            System.out.println(blockingQueue.remove());
            System.out.println(blockingQueue.remove());
    
            System.out.println(blockingQueue.remove());    //抛出异常
    
        }
    }
    

    上面只添加了3个元素,下面移除4个元素。当移除第4个元素时,抛出异常

    在这里插入图片描述

  2. offer和poll方法,有返回值,不抛出异常

    public class BlockingQueueTest {
        public static void main(String[] args) {
            test2();
        }
        
        //有返回值 不抛出异常
        public static void test2(){
            BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
            System.out.println(blockingQueue.offer(1));
            System.out.println(blockingQueue.offer(2));
            System.out.println(blockingQueue.offer(3));
            System.out.println(blockingQueue.offer(4));         //不抛出异常 返回false
    
            System.out.println("=====================================");
    
            System.out.println(blockingQueue.poll());
            System.out.println(blockingQueue.poll());
            System.out.println(blockingQueue.poll());
    
            System.out.println(blockingQueue.poll());           //不抛出异常   返回null
    
        }
    }
    

    执行结果

    在这里插入图片描述

  3. 返回队首元素

    blockingQueue.element();	//返回队首元素 如果队列为null 抛出异常
    blockingQueue.peek()		//返回队首元素 如果队列为null 则返回null 不抛出异常
    
  4. put方法,队列满时阻塞,直到队列有空位为止

    public class BlockingQueueTest {
        public static void main(String[] args) throws InterruptedException {
            test3();
        }
        public static void test3() throws InterruptedException {
            BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
            blockingQueue.put(1);
            blockingQueue.put(2);
            blockingQueue.put(3);
            blockingQueue.put(4);       //程序在这里阻塞住
        }
    }
    
  5. task方法,队列为空时阻塞,直到队列有元素为止

    public class BlockingQueueTest {
        public static void main(String[] args) throws InterruptedException {
            test3();
        }
        public static void test3() throws InterruptedException {
            BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
            blockingQueue.put(1);
            blockingQueue.put(2);
            blockingQueue.put(3);
           // blockingQueue.put(4);       //程序在这里阻塞住
    
            System.out.println("======================");
    
            System.out.println(blockingQueue.take());
            System.out.println(blockingQueue.take());
            System.out.println(blockingQueue.take());
            System.out.println(blockingQueue.take());     //程序在这里阻塞住
        }
    }
    
  6. 超时等待

    public class BlockingQueueTest {
        public static void main(String[] args) throws InterruptedException {
            test4();
        }
        public static void test4() throws InterruptedException {
            BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
            System.out.println(blockingQueue.offer(1));
            System.out.println(blockingQueue.offer(2));
            System.out.println(blockingQueue.offer(3));
            System.out.println(blockingQueue.offer(4, 2, TimeUnit.SECONDS));   //等待2秒 如果加入成功返回true,否则返回false
    
            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秒 如果有值可以取就返回值,否则返回null
        }
    }
    
    
  7. 总结上面4组API

    方法抛出异常不抛异常,返回值阻塞超时阻塞
    添加addofferputoffer(obj,v,t)
    移除removepolltaskpoll(v,t)
    查看队首elementpeek
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值