DelayQueue size() method in Java with Examples
Last Updated :
06 Jul, 2021
The size() method of DelayQueue class in Java is used to return the number of elements present in the given queue. If the number of elements is greater than Integer.MAX_VALUE then Integer.MAX_VALUE is returned as the size of this DelayQueue.
Syntax:
public int size()
Parameters: It does not takes in any parameter.
Return Value: It returns an integer which is the length of the queue.
Below program illustrate the size() method of DelayQueue in Java:
Java
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String args[])
{
DelayQueue<Delayed> queue = new DelayQueue<Delayed>();
Delayed obj = new Delayed() {
public long getDelay(TimeUnit unit)
{
return 24 ;
}
public int compareTo(Delayed o)
{
if (o.getDelay(TimeUnit.DAYS) >
this .getDelay(TimeUnit.DAYS))
return 1 ;
else if (o.getDelay(TimeUnit.DAYS) ==
this .getDelay(TimeUnit.DAYS))
return 0 ;
return - 1 ;
}
};
queue.add(obj);
System.out.println( "Size : " + queue.size());
}
}
|
Share your thoughts in the comments
Please Login to comment...