DelayQueue remainingCapacity() method in Java with Examples

The remainingCapacity() method of DelayQueue always returns Integer.MAX_VALUE because a DelayQueue is not capacity constrained. That means, irrespective of the size of the DelayQueue it returns same result, i.e. Integer.MAX_VALUE.

Syntax:

public int remainingCapacity ()

Return Value: The function returns Integer.MAX_VALUE.

Exception: No exceptions are present.

Below programs illustrate the DelayQueue.remainingCapacity() method:

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate DelayQueue remainigCapacity() method
  
import java.util.concurrent.*;
import java.util.*;
  
// The DelayObject for DelayQueue
// It must implement Delayed and
// its getDelay() and compareTo() method
class DelayObject implements Delayed {
  
    private String name;
    private long time;
  
    // Contructor of DelayObject
    public DelayObject(String name, long delayTime)
    {
        this.name = name;
        this.time = System.currentTimeMillis()
                    + delayTime;
    }
  
    // Implementing getDelay() method of Delayed
    @Override
    public long getDelay(TimeUnit unit)
    {
        long diff = time - System.currentTimeMillis();
        return unit.convert(diff, TimeUnit.MILLISECONDS);
    }
  
    // Implementing compareTo() method of Delayed
    @Override
    public int compareTo(Delayed obj)
    {
        if (this.time < ((DelayObject)obj).time) {
            return -1;
        }
        if (this.time > ((DelayObject)obj).time) {
            return 1;
        }
        return 0;
    }
  
    // Implementing toString() method of Delayed
    @Override
    public String toString()
    {
        return "\n{"
            + " " + name + ", time=" + time + "}";
    }
}
  
// Driver Class
public class GFG {
    public static void main(String[] args) throws InterruptedException
    {
  
        // create object of DelayQueue
        // using DelayQueue() constructor
        BlockingQueue<DelayObject> DQ
            = new DelayQueue<DelayObject>();
  
        // Add numbers to end of DelayQueue
        // using add() method
        DQ.add(new DelayObject("A", 1));
        DQ.add(new DelayObject("B", 2));
  
        // print the size of DelayQueue
        System.out.println("Size of the DelayQueue: "
                           + DQ.size());
  
        // remainigCapacity() method always returns Integer.MAX_VALUE
        System.out.println("DelayQueue Remaining Capacity:"
                           + DQ.remainingCapacity());
        // poll() method for removing head of the DelayQueue
        DQ.poll();
        // print the size of DelayQueue
        System.out.println("Size of the DelayQueue: "
                           + DQ.size());
        // remainigCapacity() method always returns Integer.MAX_VALUE
        System.out.println("DelayQueue Remaining Capacity:"
                           + DQ.remainingCapacity());
        // poll() method for removing head of the DelayQueue
        DQ.poll();
        // print the size of DelayQueue
        System.out.println("Size of the DelayQueue: "
                           + DQ.size());
        // remainigCapacity() method always returns Integer.MAX_VALUE
        System.out.println("DelayQueue Remaining Capacity:"
                           + DQ.remainingCapacity());
    }
}

chevron_right


Output:

Size of the DelayQueue: 2
DelayQueue Remaining Capacity:2147483647
Size of the DelayQueue: 1
DelayQueue Remaining Capacity:2147483647
Size of the DelayQueue: 0
DelayQueue Remaining Capacity:2147483647


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.