import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class BlockingQueueExample { public static void main(String[] args) { BlockingQueuequeue = new ArrayBlockingQueue<>(10); System.out.println("Remaining capacity: " + queue.remainingCapacity()); } }
Remaining capacity: 10
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class BlockingQueueExample { public static void main(String[] args) throws InterruptedException { BlockingQueuequeue = new ArrayBlockingQueue<>(10); queue.put("one"); queue.put("two"); System.out.println("Remaining capacity: " + queue.remainingCapacity()); } }
Remaining capacity: 8In this example, we create a BlockingQueue using the ArrayBlockingQueue implementation with an initial capacity of 10. We then add two elements to the queue using the put method, which blocks if the queue is full. After adding the elements, we call remainingCapacity to get the remaining capacity of the queue, which should be 8 since we added two elements. Overall, the remainingCapacity method is a useful tool for determining how much capacity is left in a BlockingQueue and whether additional elements can be added without blocking. It is part of the java.util.concurrent package library.