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


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


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

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

take()方法用於檢索和刪除此隊列的頭。如果隊列為空,則它將等待直到元素可用。

用法:


public E take()throws InterruptedException

參數:該方法不帶任何參數。

返回值:該方法返回此隊列開頭的值。

異常:如果在等待時被中斷,該方法將拋出InterruptedException。

以下示例程序旨在說明ArrayBlockingQueue的take()方法:
程序1

// Program to demonstrate take() method of ArrayBlockingQueue 
  
import java.util.concurrent.ArrayBlockingQueue; 
  
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 elements to ArrayBlockingQueue 
    queue.add(23); 
    queue.add(32); 
    queue.add(45); 
    queue.add(12); 
      
    // Print queue after adding numbers 
    System.out.print("After addding numbers Queue: "
                                                +queue); 
  
        // Apply take() method 
    int head=queue.take(); 
      
    // Print head of queue using take() method 
    System.out.println("Head of queue removed is "
                                                +head); 
    System.out.print("After removing head. Queue: "); 
    System.out.println(queue); 
      
    // Apply take() method 
    head = queue.take(); 
      
    // Print head of queue using take() method 
    System.out.println("Head of queue removed is " 
                                               + head); 
    System.out.print("After removing head. Queue: "); 
    System.out.println(queue); 
      
}  
}
輸出:
After addding numbers Queue: [23, 32, 45, 12]Head of queue removed is 23
After removing head. Queue: [32, 45, 12]
Head of queue removed is 32
After removing head. Queue: [45, 12]

示例2:

// Program to demonstrate take() method of ArrayBlockingQueue 
  
import java.util.concurrent.ArrayBlockingQueue; 
  
public class GFG { 
  
    // Create a User Object with name  
    // and age as the 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)  
                        throws InterruptedException 
    { 
        GFG gfg = new GFG(); 
        gfg.takeMethodExample(); 
    } 
  
    // Method to give example of take function 
    public void takeMethodExample()  
                        throws InterruptedException 
    { 
  
        // define 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 
        queue.offer(user1); 
        queue.offer(user2); 
        queue.offer(user3); 
        queue.offer(user4); 
        queue.offer(user5); 
  
        // find take() of queue 
        User head = queue.take(); 
  
        // print head 
        System.out.println("Details of User Removed"
                    +" After Applying take() Method"); 
        System.out.println("User Name : " + head.name); 
        System.out.println("User Age : " + head.age); 
  
        // find take() of queue 
        head = queue.take(); 
  
        // print head 
        System.out.println("Details of User Removed"+ 
                      " After Applying take() Method"); 
        System.out.println("User Name : " + head.name); 
        System.out.println("User Age : " + head.age); 
    } 
}
輸出:
Details of User Removed After Applying take() Method
User Name : Aman
User Age : 24
Details of User Removed After Applying take() Method
User Name : Amar
User Age : 23

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



相關用法


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