Java 中的 ArrayBlockingQueue remainingCapacity() 方法

java 8object oriented programmingprogramming

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

相关文章