日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

詳解BlockingQueue阻塞隊列的使用

作者:wu1308156206 更新時間: 2022-07-10 編程語言

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

    方法 拋出異常 不拋異常,返回值 阻塞 超時阻塞
    添加 add offer put offer(obj,v,t)
    移除 remove poll task poll(v,t)
    查看隊首 element peek

原文鏈接:https://blog.csdn.net/wu1308156206/article/details/125688819

相關推薦

欄目分類
最近更新