Java DelayQueue remove()方法

Java DelayQueue remove()方法

Java中DelayQueue类的remove()方法用于从这个DelayQueue中删除给定对象的一个实例,即obj,如果它存在的话。如果给定的元素被成功删除,它将返回true,否则将返回false。

语法:

public boolean remove(Object obj)

参数。这个方法接收一个要从这个DealyQueue中删除的对象 obj 作为参数。

返回值。它返回一个布尔值,如果该元素被成功删除,该值为真,否则返回假。

下面的程序说明了Java中DelayQueue的remove()方法。

// Java Program to illustrate the remove method
// of DelayQueue class
  
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
  
public class GFG {
    public static void main(String args[])
    {
        // Create a DelayQueue instance
        DelayQueue<Delayed> queue = new DelayQueue<Delayed>();
  
        // Create an object of type Delayed
        Delayed ob = new Delayed() {
            public long getDelay(TimeUnit unit)
            {
                return 24; // some value is returned
            }
  
            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;
            }
        };
  
        // Add the object to DelayQueue
        queue.add(ob);
  
        // Print initial size of Queue
        System.out.println("Initial Size : "
                           + queue.size());
  
        // Remove the object ob from
        // this DelayQueue
        queue.remove(ob);
  
        // Print the final size of the DelayQueue
        System.out.println("Size after removing : "
                           + queue.size());
    }
}

输出。

Initial Size : 1
Size after removing : 0

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程