當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ArrayBlockingQueue offer()用法及代碼示例


ArrayBlockingQueue是有界的阻塞隊列,該隊列內部使用數組支持的元素存儲。

  • ArrayBlockingQueue類是Java Collections Framework的成員。
  • 有界意味著它將具有固定的大小,您不能存儲數量超過隊列容量的元素。
  • 隊列還遵循FIFO(先進先出)規則,用於存儲和刪除隊列中的元素。
  • 如果您嘗試將一個元素放入一個完整的隊列或從一個空隊列中取出一個元素,那麽該隊列將阻塞。

根據傳遞給它的參數,offer()方法有兩種類型:

  1. 如果隊列未滿,則offer(E element)方法將作為參數傳遞的元素插入此隊列(ArrayBlockingQueue)的末尾。當添加操作成功時,它返回true;如果此隊列已滿,則返回false。此方法優於add()方法,因為在隊列已滿時add方法會引發錯誤,但在這種情況下offer()方法將返回false。
    用法:
    public boolean offer(E e)

    參數:該方法采用一個參數,即元素。這是指要添加到隊列中的元素。

    返回值:當添加操作成功時,此方法返回True;如果此隊列已滿,則返回false。

    異常如果指定的元素為null,則此方法將引發NullPointerException。

    以下示例程序旨在說明ArrayBlockingQueue的offer(E元素)方法:
    示例1:

    // Demonstrating offer(E element) method of ArrayBlockingQueue 
      
    import java.util.concurrent.ArrayBlockingQueue; 
      
    public class GFG { 
      
    public static void main(String[] args) { 
        // Define capacity of ArrayBlockingQueue 
        int capacity = 5; 
          
        // Create object of ArrayBlockingQueue 
        ArrayBlockingQueue<Integer> queue =  
              new ArrayBlockingQueue<Integer>(capacity); 
          
        // Add 5 elements to ArrayBlockingQueue 
        System.out.println("adding 423: "+queue.offer(423)); 
        System.out.println("adding 243: "+queue.offer(243)); 
        System.out.println("adding 237: "+queue.offer(237)); 
        System.out.println("adding 867: "+queue.offer(867)); 
        System.out.println("adding 23: "+queue.offer(23)); 
          
        // Check whether queue is full or not 
        if(queue.remainingCapacity()==0) { 
            System.out.println("queue is full"); 
            System.out.println("queue contains "+queue); 
        }else { 
            System.out.println("queue is not full"); 
            System.out.println("queue contains "+queue); 
        } 
          
        // Try to add more elements 
        System.out.println("adding 123: "+queue.offer(123)); 
        System.out.println("adding 321: "+queue.offer(321)); 
          
    }  
    }
    輸出:
    adding 423: true
    adding 243: true
    adding 237: true
    adding 867: true
    adding 23: true
    queue is full
    queue contains [423, 243, 237, 867, 23]
    adding 123: false
    adding 321: false
    

    示例2:

    // Program Demonstrate offer(E e) method of ArrayBlockingQueue 
      
    import java.util.concurrent.ArrayBlockingQueue; 
      
    public class GFG { 
      
        // create a User Object with name and age as an attribute 
        public class User { 
      
            public String name; 
            public String age; 
            User(String name, String age) 
            { 
                this.name = name; 
                this.age = age; 
            } 
        } 
      
        // Main Method 
        public static void main(String[] args) 
        { 
            GFG gfg = new GFG(); 
            gfg.offerExample(); 
        } 
      
        // Method to give example of contains function 
        public void offerExample() 
        { 
      
            // Define the capacity of ArrayBlockingQueue 
            int capacity = 5; 
      
            // Create object of ArrayBlockingQueue 
            ArrayBlockingQueue<User> queue =  
                    new ArrayBlockingQueue<User>(capacity); 
      
            // Create user objects 
            User user1 = new User("Aman", "24"); 
            User user2 = new User("Amar", "23"); 
            User user3 = new User("Sanjeet", "25"); 
            User user4 = new User("Suvo", "26"); 
            User user5 = new User("Ravi", "22"); 
      
            // Add Objects to ArrayBlockingQueue 
            System.out.println("adding user having name = "
                    + user1.name + ": " + queue.offer(user1)); 
            System.out.println("adding user having name = "
                    + user2.name + ": " + queue.offer(user2)); 
            System.out.println("adding user having name = "
                    + user3.name + ": " + queue.offer(user3)); 
            System.out.println("adding user having name = "
                    + user4.name + ": " + queue.offer(user4)); 
            System.out.println("adding user having name = "
                    + user5.name + ": " + queue.offer(user5)); 
      
            // Check whether the queue is full or not 
            if (queue.remainingCapacity() == 0) { 
                System.out.println("queue is full"); 
            } 
            else { 
                System.out.println("queue is not full"); 
            } 
      
            // Create more user objects 
            User user6 = new User("Ram", "20"); 
            User user7 = new User("Mohan", "27"); 
      
            // Add users in queue 
            System.out.println("adding user having name = "
                    + user6.name + ": " + queue.offer(user6)); 
            System.out.println("adding user having name = "
                    + user7.name + ": " + queue.offer(user7)); 
        } 
    }
    輸出:
    adding user having name = Aman: true
    adding user having name = Amar: true
    adding user having name = Sanjeet: true
    adding user having name = Suvo: true
    adding user having name = Ravi: true
    queue is full
    adding user having name = Ram: false
    adding user having name = Mohan: false
    
  2. 如果隊列未滿,則offer(E element, long timeout, TimeUnit unit)方法會將作為參數傳遞的元素插入此隊列(ArrayBlockingQueue)的末尾。如果隊列已滿,它將等待指定的時間以使空間可用。當添加操作成功時,它將返回true;如果此隊列已滿,並且在空間可用之前經過了指定的等待時間,則返回false。當我們要等待隊列刪除其元素並且隊列未滿時,此方法很有用,但是該元素將被添加到隊列中,但是我們可以等待特定的時間,並且這個時間可以由我們定義。

    用法:

    public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException

    參數:該方法具有三個參數:

    • element(對象)-要添加到隊列中的元素。
    • timeout(long)-放棄之前要等待的時間,以單位為單位。
    • unit(TimeUnit)-確定如何解釋超時參數的TimeUnit。

    返回值:如果添加方法成功,則該方法返回True;如果在空間可用之前經過了指定的等待時間,則返回false。

    異常:該方法引發兩種類型的異常:

    • InterruptedException–如果在等待時被打斷。
    • NullPointerException –如果指定的元素為null。

    以下示例程序旨在說明ArrayBlockingQueue的offer(E元素,長超時,TimeUnit單位)方法:

    // Program Demonstrate offer(E e, long timeout, TimeUnit unit) 
    // method of ArrayBlockingQueue 
      
    import java.util.concurrent.ArrayBlockingQueue; 
    import java.util.concurrent.TimeUnit; 
      
    public class GFG { 
      
        public static void main(String[] args)  
                      throws InterruptedException 
        { 
            // Define capacity of ArrayBlockingQueue 
            int capacity = 5; 
      
            // Create object of ArrayBlockingQueue 
            ArrayBlockingQueue<Integer> queue =  
                new ArrayBlockingQueue<Integer>(capacity); 
      
            // Add 5 elements to ArrayBlockingQueue having 
            // Timeout in seconds with value 10 secs 
            System.out.println("adding 423: " +  
                  queue.offer(433, 10, TimeUnit.SECONDS)); 
            System.out.println("adding 456: " +  
                  queue.offer(456, 10, TimeUnit.SECONDS)); 
            System.out.println("adding 987: " +  
                  queue.offer(987, 10, TimeUnit.SECONDS)); 
            System.out.println("adding 578: " +  
                  queue.offer(578, 10, TimeUnit.SECONDS)); 
            System.out.println("adding 852: " +  
                  queue.offer(852, 10, TimeUnit.SECONDS)); 
      
            // Check whether queue is full or not 
            if (queue.remainingCapacity() == 0) { 
                System.out.println("queue is full"); 
                System.out.println("queue contains " + queue); 
            } 
            else { 
                System.out.println("queue is not full"); 
                System.out.println("queue contains " + queue); 
            } 
      
            // Try to add more elements 
            System.out.println("adding 546: " +  
                      queue.offer(546, 10, TimeUnit.SECONDS)); 
        } 
    }
    輸出:
    adding 423: true
    adding 456: true
    adding 987: true
    adding 578: true
    adding 852: true
    queue is full
    queue contains [433, 456, 987, 578, 852]
    adding 546: false
    

參考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#offer(E)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ArrayBlockingQueue offer() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。