ArrayBlockingQueue es una cola de bloqueo limitada que almacena los elementos respaldados internamente por una array.
- La clase ArrayBlockingQueue es miembro de Java Collections Framework.
- Acotado significa que tendrá un tamaño fijo, no puede almacenar el número de elementos más que la capacidad de la cola.
- La cola también sigue la regla FIFO (primero en entrar, primero en salir) para almacenar y eliminar elementos de la cola.
- Si intenta colocar un elemento en una cola llena o tomar un elemento de una cola vacía, la cola lo bloqueará.
El método take() se usa para recuperar y eliminar el encabezado de esta cola. Si la cola está vacía, esperará hasta que un elemento esté disponible. Sintaxis:
public E take()throws InterruptedException
Parámetros: El método no toma ningún parámetro.
Valor devuelto: el método devuelve el valor al principio de esta cola.
Excepción: el método arroja InterruptedException si se interrumpe mientras espera.
Los siguientes programas ilustran el método take() de ArrayBlockingQueue:
Programa 1 :
Java
// 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 adding 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 adding 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]
Programa 2:
Java
// 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
Referencia: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#take()
Publicación traducida automáticamente
Artículo escrito por AmanSingh2210 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA