Java DelayQueue drainTo()方法及示例
DelayQueue的 **drainTo(Collection <E> c) **方法从这个DelayQueue中移除所有可用的元素,并将它们添加到作为参数传递的给定集合中。这个方法比反复轮询这个DelayQueue更有效。
,也有失败的可能。如果一个DelayQueue试图将一个队列排到自己身上,将会导致IllegalArgumentException。此外,如果在操作过程中修改了指定的集合,那么这个操作的行为就无法定义。
语法:
public int drainTo (Collection <E> c)
参数: 该方法接受一个参数c,代表要从DelayQueue转移元素的集合。
返回值: 该函数返回转移元素的数量。
异常: 该方法抛出以下异常:
- UnsupportedOperationException – 如果集合不能添加元素。
- ClassCastException – 如果元素的类别阻止了向集合添加元素的方法。
- NullPointerException – 如果集合为空。
- 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;
// Constructor 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;
// Constructor 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 col, int maxElements)
**drainTo(Collection
语法:
drainTo(Collection <E> c, int maxElements)
参数: 该方法接受两个参数:-
- c – 它代表要从DelayQueue.s转移元素的集合。
- maxElements – 这是一个整数类型的参数,指的是要转移到集合中的最大元素数。
返回值: 该函数返回转移的元素数量。
异常: 该方法抛出以下异常:
- UnsupportedOperationException – 如果集合不能添加元素。
- ClassCastException – 如果元素的类别阻止了向集合添加元素的方法。
- NullPointerException – 如果集合为空。
- IllegalArgumentException – 如果方法的参数阻止它被添加到指定的集合中。
下面的程序说明了DelayQueue类的 drainTo(Collection
**Program :
// 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;
// Constructor 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}]
极客教程