El método de capacidad restante() de LinkedBlockingQueue devuelve la cantidad de elementos adicionales que se pueden agregar a LinkedBlockingQueue sin bloquear.
La Capacidad devuelta se da en tres casos:
- Si la capacidad restante es cero, no se pueden agregar más elementos a LinkedBlockingQueue.
- Si la capacidad restante de LinkedBlockingQueue es igual al tamaño de la cola, entonces no se puede eliminar ningún elemento de la cola porque, en tal situación, la cola está vacía.
- En cualquier otro caso, la capacidad siempre es igual a la diferencia entre la capacidad inicial de este LinkedBlockinQueue y el tamaño actual de este LinkedBlockingQueue.
Sintaxis:
public int remainingCapacity()
Valor devuelto: este método devuelve la capacidad restante de LinkedBlockingQueue.
Los siguientes programas ilustran el método de capacidad restante() de la clase LinkedBlockingQueue:
Programa 1: devolver la capacidad restante de LinkedBlockingQueue usando el método de capacidad restante() donde LinkedBlockingQueue contiene una lista de nombres.
// Java Program Demonstrate remainingCapacity() // method of LinkedBlockingQueue import java.util.concurrent.LinkedBlockingQueue; public class GFG { public static void main(String[] args) { // define capacity of LinkedBlockingQueue int capacityOfQueue = 7; // create object of LinkedBlockingQueue LinkedBlockingQueue<String> linkedQueue = new LinkedBlockingQueue<String>(capacityOfQueue); // Add element to LinkedBlockingQueue linkedQueue.add("John"); linkedQueue.add("Tom"); linkedQueue.add("Clark"); linkedQueue.add("Kat"); // find remaining Capacity of linkedQueue // using remainingCapacity() method int remainingCapacity = linkedQueue.remainingCapacity(); // print result System.out.println("Queue is " + linkedQueue); // print head of queue System.out.println("Remaining Capacity of Queue is " + remainingCapacity); } }
Queue is [John, Tom, Clark, Kat] Remaining Capacity of Queue is 3
Programa 2: encontrar la capacidad restante de LinkedBlockingQueue que contiene una lista de empleados.
// Java Program Demonstrate peek() // method of LinkedBlockingQueue import java.util.concurrent.LinkedBlockingQueue; public class GFG { public void findPeek() { // define capacity of LinkedBlockingQueue int capacityOfQueue = 7; // create object of LinkedBlockingQueue LinkedBlockingQueue<Employee> linkedQueue = new LinkedBlockingQueue<Employee>(capacityOfQueue); // Add element to LinkedBlockingQueue Employee emp1 = new Employee("Ravi", "Tester", "39000"); Employee emp2 = new Employee("Sanjeet", "Manager", "98000"); // Add Employee Objects to linkedQueue linkedQueue.add(emp1); linkedQueue.add(emp2); // add element and find remaining capacity // follow same process again // until the queue becomes full while (linkedQueue.size() != 7) { // adding emp2 again and again to queue System.out.println("Adding employee is success " + linkedQueue.offer(emp2)); // find remaining capacity of linkedQueue // using remainingCapacity() method int remain = linkedQueue.remainingCapacity(); // print remaining capacity value System.out.println("Remaining Capacity of list :" + remain); } } // create an Employee Object with name, // position and salary as an attribute public class Employee { public String name; public String position; public String salary; Employee(String name, String position, String salary) { this.name = name; this.position = position; this.salary = salary; } @Override public String toString() { return "Employee [name=" + name + ", position=" + position + ", salary=" + salary + "]"; } } // Main Method public static void main(String[] args) { GFG gfg = new GFG(); gfg.findPeek(); } }
Adding employee is success true Remaining Capacity of list :4 Adding employee is success true Remaining Capacity of list :3 Adding employee is success true Remaining Capacity of list :2 Adding employee is success true Remaining Capacity of list :1 Adding employee is success true Remaining Capacity of list :0
Referencia: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#remainingCapacity–
Publicación traducida automáticamente
Artículo escrito por AmanSingh2210 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA