📜  Java中的 ArrayBlockingQueue take() 方法

📅  最后修改于: 2022-05-13 01:55:08.980000             🧑  作者: Mango

Java中的 ArrayBlockingQueue take() 方法

ArrayBlockingQueue是有界的阻塞队列,它在内部存储由数组支持的元素。

  • ArrayBlockingQueue类是Java集合框架的成员。
  • 有界意味着它将具有固定大小,您不能存储数量超过队列容量的元素。
  • 队列还遵循 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 queue = 
        new ArrayBlockingQueue(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]

方案二:

// 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 queue = 
               new ArrayBlockingQueue(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: Java/util/concurrent/ArrayBlockingQueue.html#take()