ArrayBlockingQueue remainingCapacity() Method in Java

Java 8Object Oriented ProgrammingProgramming

The remainingCapacity() method of the ArrayBlockingQueue class in Java is used to return the number of additional elements that the queue can adopt without blocking.

The syntax is as follows

int remainingCapacity()

To work with ArrayBlockingQueue class, you need to import the following package

import java.util.concurrent.ArrayBlockingQueue;

The following is an example to implement remainingCapacity() method of Java ArrayBlockingQueue class

Example

 Live Demo

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());
   }
}

Output

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
raja
Published on 28-Mar-2019 08:52:41
Advertisements