El método de capacidad restante de PriorityBlockingQueue se usa para verificar cuántos elementos más se pueden insertar en esta cola. Pero dado que PriorityBlockingQueue no tiene límites, este método siempre devuelve Integer.MAX_VALUE porque PriorityBlockingQueue no tiene restricciones de capacidad.
Sintaxis:
public int remainingCapacity()
Valor de retorno: Integer.MAX_VALUE siempre Los siguientes
programas ilustran el método de capacidad restante() en PriorityBlockingQueue:
Programa 1:
Java
// Java Program Demonstrate remainingCapacity() // method of PriorityBlockingQueue import java.util.concurrent.PriorityBlockingQueue; public class GFG { public static void main(String[] args) { // define capacity of PriorityBlockingQueue int capacityOfQueue = 7; // create object of PriorityBlockingQueue PriorityBlockingQueue<Integer> pbq = new PriorityBlockingQueue<Integer>(capacityOfQueue); // Add element to PriorityBlockingQueue pbq.put(1); pbq.put(2); pbq.put(3); pbq.put(4); // find remaining Capacity of pbq // using remainingCapacity() method // The initial capacity was set to 7 // by passing as parameter in constructor // But this method will return Integer.MAX_VALUE int remainingCapacity = pbq.remainingCapacity(); // print result System.out.println("Queue: " + pbq); System.out.println("Remaining Capacity: " + remainingCapacity); } }
Producción:
Queue: [1, 2, 3, 4] Remaining Capacity: 2147483647
Programa 2: Para demostrar la capacidad restante() usando String
Java
// Java Program Demonstrate remainingCapacity() // method of PriorityBlockingQueue import java.util.concurrent.PriorityBlockingQueue; public class GFG { public static void main(String[] args) { // create object of PriorityBlockingQueue PriorityBlockingQueue<String> pbq = new PriorityBlockingQueue<String>(); // Add element to PriorityBlockingQueue pbq.put("Geeks"); pbq.put("forGeeks"); pbq.put("A Computer"); pbq.put("Portal"); // find remaining Capacity of pbq // using remainingCapacity() method int remainingCapacity = pbq.remainingCapacity(); // print result System.out.println("Queue: " + pbq); System.out.println("Remaining Capacity: " + remainingCapacity); } }
Producción:
Queue: [A Computer, Portal, Geeks, forGeeks] Remaining Capacity: 2147483647