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


Java DelayQueue poll()用法及代碼示例


DelayQueue的poll()方法用於檢索DelayQueue的頭部。此方法還從DelayQueue中刪除頭。因此,當調用poll方法時,DelayQueue的大小將減小1。

用法:

public E poll ()

參數:
此方法不接受任何參數。


返回值:
此方法返回DelayQueue的頭部,並將其從此DelayQueue中刪除。

異常:
NullPointerException :如果head不存在,則此函數將返回null。

下麵的程序來說明Java中的DelayQueue poll():

例:

// Java Program Demonstrate DelayQueue poll() method 
  
import java.util.concurrent.*; 
import java.util.*; 
  
// The DelayObject for DelayQueue 
// It must implement Delayed and 
// its getDelay() and compareTo() method 
class DelayObject implements Delayed { 
  
    private String name; 
    private long time; 
  
    // Contructor of DelayObject 
    public DelayObject(String name, long delayTime) 
    { 
        this.name = name; 
        this.time = System.currentTimeMillis() 
                    + delayTime; 
    } 
  
    // Implementing getDelay() method of Delayed 
    @Override
    public long getDelay(TimeUnit unit) 
    { 
        long diff = time - System.currentTimeMillis(); 
        return unit.convert(diff, TimeUnit.MILLISECONDS); 
    } 
  
    // Implementing compareTo() method of Delayed 
    @Override
    public int compareTo(Delayed obj) 
    { 
        if (this.time < ((DelayObject)obj).time) { 
            return -1; 
        } 
        if (this.time > ((DelayObject)obj).time) { 
            return 1; 
        } 
        return 0; 
    } 
  
    // Implementing toString() method of Delayed 
    @Override
    public String toString() 
    { 
        return "\n{"
            + " " + name + ", time=" + time + "}"; 
    } 
} 
  
// Driver Class 
public class GFG { 
    public static void main(String[] args) throws InterruptedException 
    { 
  
        // create object of DelayQueue 
        // using DelayQueue() constructor 
        BlockingQueue<DelayObject> DQ 
            = new DelayQueue<DelayObject>(); 
  
        // Add numbers to end of DelayQueue 
        // using add() method 
        DQ.add(new DelayObject("A", 1)); 
        DQ.add(new DelayObject("B", 2)); 
        DQ.add(new DelayObject("C", 3)); 
        DQ.add(new DelayObject("D", 4)); 
  
        // Print delayqueue 
        System.out.println("Original DelayQueue: "
                           + DQ + "\n"); 
        // Print size of original DelayQueue 
        System.out.println("Original DelayQueue size: "
                           + DQ.size() + "\n"); 
  
        // poll() method for returning head of the DelayQueue 
        System.out.println("Head of the DelayQueue: "
                           + DQ.poll()); 
        // print DelayQueue size after poll method 
        System.out.println("\nAfter poll() method, DelayQueue size: "
                           + DQ.size()); 
    } 
}
輸出:
Original DelayQueue: [
{ A, time=1545817370442}, 
{ B, time=1545817370443}, 
{ C, time=1545817370444}, 
{ D, time=1545817370445}]

Original DelayQueue size: 4

Head of the DelayQueue: 
{ A, time=1545817370442}

After poll() method, DelayQueue size: 3

例:

// Java Program Demonstrate DelayQueue poll() method 
  
import java.util.concurrent.*; 
import java.util.*; 
  
// The DelayObject for DelayQueue 
// It must implement Delayed and 
// its getDelay() and compareTo() method 
class DelayObject implements Delayed { 
  
    private String name; 
    private long time; 
  
    // Contructor of DelayObject 
    public DelayObject(String name, long delayTime) 
    { 
        this.name = name; 
        this.time = System.currentTimeMillis() 
                    + delayTime; 
    } 
  
    // Implementing getDelay() method of Delayed 
    @Override
    public long getDelay(TimeUnit unit) 
    { 
        long diff = time - System.currentTimeMillis(); 
        return unit.convert(diff, TimeUnit.MILLISECONDS); 
    } 
  
    // Implementing compareTo() method of Delayed 
    @Override
    public int compareTo(Delayed obj) 
    { 
        if (this.time < ((DelayObject)obj).time) { 
            return -1; 
        } 
        if (this.time > ((DelayObject)obj).time) { 
            return 1; 
        } 
        return 0; 
    } 
  
    // Implementing toString() method of Delayed 
    @Override
    public String toString() 
    { 
        return "\n{"
            + " " + name + ", time=" + time + "}"; 
    } 
} 
  
// Driver Class 
public class GFG { 
    public static void main(String[] args) throws InterruptedException 
    { 
  
        // create object of DelayQueue 
        // using DelayQueue() constructor 
        BlockingQueue<DelayObject> DQ 
            = new DelayQueue<DelayObject>(); 
  
        // Print size of original DelayQueue 
        System.out.println("Original DelayQueue size: "
                           + DQ.size() + "\n"); 
  
        // poll() method for returning head of the DelayQueue 
        System.out.println("Head of the DelayQueue: "
                           + DQ.poll()); 
    } 
}
輸出:
Original DelayQueue size: 0

Head of the DelayQueue: null


相關用法


注:本文由純淨天空篩選整理自ProgrammerAnvesh大神的英文原創作品 DelayQueue poll() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。