DelayQueue clear() method in Java
The clear() method of DelayQueue in Java is used to remove all of the elements in the current DelayQueue object. The queue will be empty after this call is returned. The elements in the queue whose Delay does not have an Expiry are automatically discarded from the Queue.
Syntax:
public void clear()
Parameters: This method does not takes any parameter.
Return Value: This method does not returns any value.
Below program illustrate the above method:
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("Is queue empty() : "
+ queue.isEmpty());
queue.clear();
System.out.println("Is queue empty after calling clear() : "
+ queue.isEmpty());
}
}
|
Output:
Is queue empty() : false
Is queue empty after calling clear() : true
Last Updated :
14 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...