Java 中 ArrayBlockingQueue 类的 remainingCapacity ()方法用于返回队列可以在不阻塞的情况下采用的附加元素的数量。
语法如下
int remainingCapacity()
要使用 ArrayBlockingQueue 类,您需要导入以下包
import java.util.concurrent.ArrayBlockingQueue;
下面是一个实现Java ArrayBlockingQueue类的remainingCapacity()方法的示例
import java.util.concurrent.ArrayBlockingQueue; public class Demo { public static void main(String[] args) throws InterruptedException { ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(10); q.add(120); q.add(400); q.add(450); q.add(500); System.out.println("ArrayBlockingQueue = " + q); System.out.println("ArrayBlockingQueue Remaining Capacity = " + q.remainingCapacity()); q.add(620); q.add(700); System.out.println("ArrayBlockingQueue = " + q); System.out.println("ArrayBlockingQueue Remaining Capacity = " + q.remainingCapacity()); q.add(820); q.add(900); q.add(950); System.out.println("ArrayBlockingQueue = " + q); System.out.println("ArrayBlockingQueue Remaining Capacity = " + q.remainingCapacity()); } }
输出结果
ArrayBlockingQueue = [120, 400, 450, 500] ArrayBlockingQueue Remaining Capacity = 6 ArrayBlockingQueue = [120, 400, 450, 500, 620, 700] ArrayBlockingQueue Remaining Capacity = 4 ArrayBlockingQueue = [120, 400, 450, 500, 620, 700, 820, 900, 950] ArrayBlockingQueue Remaining Capacity = 1