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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。