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


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


DelayQueue的drainTo(Collection c)方法從該DelayQueue中刪除所有可用元素,並將它們添加到作為參數傳遞的給定集合中。此方法比重複輪詢此DelayQueue更有效。

還有失敗的可能性。如果DelayQueue嘗試將隊列耗盡到自身,將導致IllegalArgumentException。此外,如果在操作進行過程中修改了指定的集合,則此操作的行為不確定。

用法:


public int drainTo (Collection<E> c)

參數:此方法接受一個參數c,該參數表示要從DelayQueue傳輸元素的集合。

返回值:該函數返回傳送的元素數。

異常:此方法引發以下異常:

  • UnsupportedOperationException–如果collection無法添加元素。
  • ClassCastException–如果元素類停止將元素添加到集合的方法。
  • NullPointerException –如果集合為null。
  • IllegalArgumentException–如果方法的參數阻止將其添加到指定的集合中。

以下示例程序旨在說明DelayQueue.drainTo()方法:

示例1:

// Java Program Demonstrate DelayQueue drainTo() 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)); 
  
        System.out.println("Before drainTo():"); 
        System.out.println("DelayQueue: " + DQ); 
  
        // create a ArrayList to pass as parameter to drainTo() 
        ArrayList<DelayObject> array 
            = new ArrayList<DelayObject>(); 
  
        // Apply drainTo method and pass array as parameter 
        int response = DQ.drainTo(array); 
        // print no of element passed 
        System.out.println("\nNo of element passed: "
                           + response); 
  
        // printing Arraylist and deque 
        // after applying drainTo() method 
        System.out.println("\nAfter drainTo():"); 
        System.out.println("DelayQueue : \n"
                           + DQ); 
        System.out.println("ArrayList : \n"
                           + array); 
    } 
}
輸出:
Before drainTo():
DelayQueue: [
{ A, time=1546842375114}, 
{ B, time=1546842375115}, 
{ C, time=1546842375116}, 
{ D, time=1546842375117}]

No of element passed: 4

After drainTo():
DelayQueue : 
[]
ArrayList : 
[
{ A, time=1546842375114}, 
{ B, time=1546842375115}, 
{ C, time=1546842375116}, 
{ D, time=1546842375117}]

程序2:顯示drainTo()方法引發的異常的程序。

// Java Program Demonstrate DelayQueue drainTo() 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)); 
  
        // create a collection with null 
        ArrayList<DelayObject> collection = null; 
  
        // try to drain null DelayQueue to collection 
        try { 
            DQ.drainTo(collection); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
Exception: java.lang.NullPointerException

drainTo(Collection<E> col, int maxElements)

rainageTo(Collection col,int maxElements)方法從此隊列中最多移除給定數量的可用元素,並將它們添加到給定的collection中。傳輸元素後,DelayQueue僅具有那些未傳輸到集合的元素。

用法:


drainTo(Collection<E> c, int maxElements)

參數:此方法接受兩個參數:

  • c–它表示要從DelayQueue.s傳輸元素的集合。
  • maxElements–這是整數類型,是指要傳輸到集合的最大元素數。

返回值:該函數返回傳送的元素數。

異常:此方法引發以下異常:

  • UnsupportedOperationException–如果collection無法添加元素。
  • ClassCastException–如果元素類停止將元素添加到集合的方法。
  • NullPointerException –如果集合為null。
  • IllegalArgumentException–如果方法的參數阻止將其添加到指定的集合中。

以下示例程序旨在說明DelayQueue類的raintTo(Collection col,int maxElements)方法:

程序:

// Java Program Demonstrate DelayQueue drainTo() 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)); 
  
        System.out.println("Before drainTo():"); 
        System.out.println("Number of elements in the DelayQueue: "
                           + DQ.size()); 
        System.out.println("DelayQueue: " + DQ); 
  
        // create a ArrayList to pass as parameter to drainTo() 
        ArrayList<DelayObject> array 
            = new ArrayList<DelayObject>(); 
        // Initialize no of element passed to collection 
        // using drainTo() method 
        int noOfElement = 2; 
        // Apply drainTo method and pass array as parameter 
        int response = DQ.drainTo(array, noOfElement); 
        // print no of element passed 
        System.out.println("\nNo of element passed: "
                           + response); 
  
        // printing Arraylist and deque 
        // after applying drainTo() method 
        System.out.println("\nAfter drainTo():"); 
        System.out.println("Number of elements in the DelayQueue: "
                           + DQ.size()); 
        System.out.println("DelayQueue : \n"
                           + DQ); 
        System.out.println("ArrayList : \n"
                           + array); 
    } 
}
輸出:
Before drainTo():
Number of elements in the DelayQueue: 4
DelayQueue: [
{ A, time=1546842382382}, 
{ B, time=1546842382383}, 
{ C, time=1546842382384}, 
{ D, time=1546842382385}]

No of element passed: 2

After drainTo():
Number of elements in the DelayQueue: 2
DelayQueue : 
[
{ C, time=1546842382384}, 
{ D, time=1546842382385}]
ArrayList : 
[
{ A, time=1546842382382}, 
{ B, time=1546842382383}]


相關用法


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