當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。