当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java DelayQueue remove()用法及代码示例


Java中的DelayQueue类的remove()方法用于从该DelayQueue中移除给定对象obj的单个实例(如果存在)。如果成功删除给定元素,则返回true,否则返回false。

句法

public boolean remove(Object obj)

参数:此方法将单个对象obj作为参数,该对象将从此DealyQueue中删除。


返回值:它返回一个布尔值,如果该元素已被成功删除,则返回true,否则返回false。

以下示例程序旨在说明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


相关用法


注:本文由纯净天空筛选整理自psil123大神的英文原创作品 DelayQueue remove() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。