Java中的DelayQueue drainTo()方法及示例
DelayQueue 的 drainTo(Collection
也有可能失败。如果一个DelayQueue试图将队列排空到其自身,则会导致 IllegalArgumentException 。此外,如果在操作正在进行时修改了指定的集合,则此操作的行为是未定义的。
语法:
public int drainTo (Collection <E> c)
参数: 该方法接受一个参数 c ,它代表从DelayQueue中转移元素的集合。
返回值: 该函数返回转移的元素数。
异常: 该方法会抛出以下异常:
- UnsupportedOperationException - 如果集合不能添加元素。
- ClassCastException - 如果元素的类阻止方法将元素添加到集合中。
- NullPointerException - 如果集合为null。
- IllegalArgumentException - 如果方法的参数阻止将其添加到指定的集合中。
下面的程序说明DelayQueue.drainTo()方法:
程序1:
// Java程序示例展示DelayQueue drainTo()方法
import java.util.concurrent.*;
import java.util.*;
// 延迟队列DelayObject
// 它必须实现Delayed接口,
// 和其getDelay()和compareTo()方法。
class DelayObject implements Delayed {
private String name;
private long time;
// DelayObject的构造函数
public DelayObject(String name, long delayTime)
{
this.name = name;
this.time = System.currentTimeMillis()
+ delayTime;
}
// 实现Delayed的getDelay()方法
@Override
public long getDelay(TimeUnit unit)
{
long diff = time - System.currentTimeMillis();
return unit.convert(diff, TimeUnit.MILLISECONDS);
}
// 实现Delayed的compareTo()方法
@Override
public int compareTo(Delayed obj)
{
if (this.time < ((DelayObject)obj).time) {
return -1;
}
if (this.time > ((DelayObject)obj).time) {
return 1;
}
return 0;
}
// 实现Delayed的toString()方法
@Override
public String toString()
{
return "\n{"
+ " " + name + ", time=" + time + "}";
}
}
// 驱动类
public class GFG {
public static void main(String[] args) throws InterruptedException
{
// 使用DelayQueue()构造函数创建DelayQueue对象
BlockingQueue<DelayObject> DQ
= new DelayQueue<DelayObject>();
// 使用add()方法向DelayQueue末尾添加元素
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("在drainTo()之前:");
System.out.println("DelayQueue: " + DQ);
// 使用一个ArrayList作为参数传递给drainTo()
ArrayList<DelayObject> array
= new ArrayList<DelayObject>();
// 使用drainTo()方法并传递array作为参数
int response = DQ.drainTo(array);
// 打印传递的元素数量
System.out.println("\n传递的元素数量:"
+ response);
// 在应用drainTo()方法后打印ArrayList和deque
System.out.println("\n在drainTo()之后:");
System.out.println("DelayQueue : \n"
+ DQ);
System.out.println("ArrayList : \n"
+ array);
}
}
输出:
在drainTo()之前:
DelayQueue: [
{ A, time=1546842375114},
{ B, time=1546842375115},
{ C, time=1546842375116},
{ D, time=1546842375117}]
传递的元素数量:4
在drainTo()之后:
DelayQueue :
[]
ArrayList :
[
{ A, time=1546842375114},
{ B, time=1546842375115},
{ C, time=1546842375116},
{ D, time=1546842375117}]
程序2 :演示drainTo()方法抛出的异常。
// Java程序演示DelayQueue drainTo()方法
import java.util.concurrent.*;
import java.util.*;
// DelayQueue的DelayObject
// 必须实现Delayed以及其getDelay()和compareTo()方法
class DelayObject implements Delayed {
private String name;
private long time;
// DelayObject的构造函数
public DelayObject(String name, long delayTime)
{
this.name = name;
this.time = System.currentTimeMillis()
+ delayTime;
}
// 实现Delayed的getDelay()方法
@Override
public long getDelay(TimeUnit unit)
{
long diff = time - System.currentTimeMillis();
return unit.convert(diff, TimeUnit.MILLISECONDS);
}
// 实现Delayed的compareTo()方法
@Override
public int compareTo(Delayed obj)
{
if (this.time < ((DelayObject)obj).time) {
return -1;
}
if (this.time > ((DelayObject)obj).time) {
return 1;
}
return 0;
}
// 实现Delayed的toString()方法
@Override
public String toString()
{
return "\n{"
+ " " + name + ", time=" + time + "}";
}
}
// 驱动程序类
public class GFG {
public static void main(String[] args) throws InterruptedException
{
// 使用DelayQueue()构造函数创建DelayQueue对象
BlockingQueue<DelayObject> DQ
= new DelayQueue<DelayObject>();
// 使用add()方法向DelayQueue末尾添加数字
DQ.add(new DelayObject("A", 1));
DQ.add(new DelayObject("B", 2));
DQ.add(new DelayObject("C", 3));
DQ.add(new DelayObject("D", 4));
// 创建一个带有null的集合
ArrayList<DelayObject> collection = null;
// 尝试将空的DelayQueue排入集合
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转移元素的集合。
- maxElements - 这是整数类型,指的是要转移到集合中的最大元素数。
返回值: 该函数返回转移的元素数量。
异常: 此方法抛出以下异常:
- UnsupportedOperationException - 如果集合无法添加元素。
- ClassCastException - 如果元素的类阻止方法将元素添加到集合中。
- NullPointerException - 如果集合为null。
- IllegalArgumentException - 如果方法的参数阻止其被添加到指定的集合中。
下面的程序演示了 DelayQueue 类的 drainTo(Collection
程序:
// Java程序演示 DelayQueue drainTo() 方法
import java.util.concurrent.*;
import java.util.*;
// DelayQueue 的 DelayObject
// 它必须实现 Delayed 类
// 和它的 getDelay() 和 compareTo() 方法
class DelayObject implements Delayed {
private String name;
private long time;
// DelayObject 的构造函数
public DelayObject(String name, long delayTime)
{
this.name = name;
this.time = System.currentTimeMillis()
+ delayTime;
}
// 实现 Delayed 的 getDelay() 方法
@Override
public long getDelay(TimeUnit unit)
{
long diff = time - System.currentTimeMillis();
return unit.convert(diff, TimeUnit.MILLISECONDS);
}
// 实现 Delayed 的 compareTo() 方法
@Override
public int compareTo(Delayed obj)
{
if (this.time < ((DelayObject)obj).time) {
return -1;
}
if (this.time > ((DelayObject)obj).time) {
return 1;
}
return 0;
}
// 实现 Delayed 的 toString() 方法
@Override
public String toString()
{
return "\n{"
+ " " + name + ", time=" + time + "}";
}
}
// 驱动程序
public class GFG {
public static void main(String[] args) throws InterruptedException
{
// 使用 DelayQueue() 构造函数创建 DelayQueue 对象
BlockingQueue<DelayObject> DQ
= new DelayQueue<DelayObject>();
// 使用 add() 方法向 DelayQueue 的末尾添加数字
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("在 drainTo() 方法之前:");
System.out.println("DelayQueue 中元素的数量:"
+ DQ.size());
System.out.println("DelayQueue: " + DQ);
// 创建一个 ArrayList 作为 drainTo() 的参数
ArrayList<DelayObject> array
= new ArrayList<DelayObject>();
// 使用 drainTo() 方法初始化传递给集合的元素数量
int noOfElement = 2;
// 应用 drainTo() 方法并传递 array 作为参数
int response = DQ.drainTo(array, noOfElement);
// 打印传递的元素数量
System.out.println("\n传递的元素数量:"
+ response);
// 在应用 drainTo() 方法之后打印 Arraylist 和 deque
System.out.println("\n在 drainTo() 方法之后:");
System.out.println("DelayQueue 中元素的数量:"
+ DQ.size());
System.out.println("DelayQueue:\n"
+ DQ);
System.out.println("ArrayList:\n"
+ array);
}
}
输出:
在 drainTo() 方法之前:
DelayQueue 中元素的数量:4
DelayQueue: [
{ A, time=1546842382382},
{ B, time=1546842382383},
{ C, time=1546842382384},
{ D, time=1546842382385}]
传递的元素数量:2
在 drainTo() 方法之后:
DelayQueue 中元素的数量:2
DelayQueue:
[
{ C, time=1546842382384},
{ D, time=1546842382385}]
ArrayList:
[
{ A, time=1546842382382},
{ B, time=1546842382383}]
极客教程