Java中LinkedBlockingQueue的drainTo()方法
LinkedBlockingQueue的 drainTo(Collection col) 方法会删除此LinkedBlocking Queue中的所有可用元素,并将它们添加到传递的参数集合中。
drainTo(Collection<? super E> col)
LinkedBlockingQueue的 drainTo(Collection <? super E> col)方法会删除此队列中的所有元素,并将它们添加到给定的集合col中。这比反复轮询此队列要高效。
在从队列添加元素到集合c时可能遇到失败,由于该失败,当引发关联异常时,元素将在两个集合之间分布。如果尝试将队列drainTo()到队列本身,则会抛出IllegalArgumentException。如果在操作正在进行时修改了指定的集合,则此操作的行为是未定义的。因此,要使用这种方法,需要注意这种情况以克服异常。
语法:
public int drainTo(Collection<? super E> col)
参数: 此方法接受一个参数 col ,该参数表示从LinkedBlockingQueue传输元素的集合。
返回值: 此方法返回从队列中向集合中排出的元素数。
异常: 此方法引发以下异常:
- UnsupportedOperationException - 如果集合无法添加元素。
- ClassCastException - 如果元素的类阻止方法将元素添加到集合中。
- NullPointerException - 如果集合为null
- IllegalArgumentException - 如果方法的参数阻止将其添加到指定的集合中
以下程序说明LinkedBlockingQueue类的drainTo()方法:
程序 1:
下面的程序有一个存储Employee对象的LinkedBlockingQueue。有一个ArrayList将存储LinkedBlockingQueue中的所有Employee对象。因此,在LinkedBlockingQueue中使用drainTo()将所有Employee从队列传递到ArrayList中。
// Java程序演示LinkedBlockingQueue的drainTo(Collection c)方法。
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
//创建带职位和薪水属性的雇员对象
public class Employee {
public String name;
public String position;
public String salary;
Employee(String name, String position, String salary)
{
this.name = name;
this.position = position;
this.salary = salary;
}
@Override
public String toString()
{
return "Employee [name=" + name + ", position="
+ position + ", salary=" + salary + "]";
}
}
// 主方法
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.containsMethodExample();
}
public void containsMethodExample()
{
// 定义LinkedBlockingQueue的容量
int capacity = 50;
// 创建LinkedBlockingQueue对象
LinkedBlockingQueue<Employee> linkedQueue
= new LinkedBlockingQueue<Employee>(capacity);
// 创建ArrayList,用作drainTo()的参数
ArrayList<Employee> collection = new ArrayList<Employee>();
// 将Employee对象添加到队列中
Employee emp1 = new Employee("Aman", "Analyst", "24000");
Employee emp2 = new Employee("Sachin", "Developer", "39000");
linkedQueue.add(emp1);
linkedQueue.add(emp2);
// 打印ArrayList和队列
System.out.println("Before drainTo():");
System.out.println("LinkedBlockingQueue : \n"
+ linkedQueue.toString());
System.out.println("ArrayList : \n"
+ collection);
// 使用drainTo()方法并将collection作为参数传递
int response = linkedQueue.drainTo(collection);
// 打印传递的元素数
System.out.println("\nNo of element passed: " + response);
// 在应用drainTo()方法后打印ArrayList和队列
System.out.println("\nAfter drainTo():");
System.out.println("LinkedBlockingQueue : \n"
+ linkedQueue.toString());
System.out.println("ArrayList : \n"
+ collection);
}
}
输出:
Before drainTo():
LinkedBlockingQueue :
[Employee [name=Aman, position=Analyst, salary=24000],
Employee [name=Sachin, position=Developer, salary=39000]]
ArrayList :
[]
No of element passed: 2
After drainTo():
LinkedBlockingQueue :
[]
ArrayList :
[Employee [name=Aman, position=Analyst, salary=24000], Employee [name=Sachin, position=Developer, salary=39000]]
程序2: 展示drainTo()方法抛出的异常。
// Java程序示例
// drainTo(Collection C)
// LinkedBlockingQueue的方法。
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// 定义LinkedBlockingQueue的容量
int 队列容量 = 4;
// 创建LinkedBlockingQueue的对象
LinkedBlockingQueue<Integer>
linkedQueue = new LinkedBlockingQueue<Integer>(队列容量);
// 添加元素到队列
linkedQueue.put(85461);
linkedQueue.put(44648);
linkedQueue.put(45654);
// 创建一个带有null的集合
ArrayList<Integer> 集合 = null;
// 尝试将空队列内容传输到集合中
try {
linkedQueue.drainTo(集合);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出:
Exception: java.lang.NullPointerException
drainTo(Collection<? super E> col, int maxElements)
drainTo(Collection <? super E> col, int maxElements) ** 用于将作为整数传递给drainTo()的定量元素传输到作为方法参数传递的集合中。在传输元素之后,LinkedBlockingQueue仅具有未传输到集合中的那些元素。该函数与上面的函数相同,具有将固定数量的元素传输到集合的某些限制。
**语法:
public int drainTo(Collection<? super E> col, int maxElements)
参数: 该方法接受两个参数:
- col - 它表示从LinkedBlockingQueue传输元素的集合。
- maxElements - 这是整数类型,指的是要传输到集合中的最大元素数。
返回值: 该方法返回从队列到集合的 传输的元素数 。
异常: 该方法会抛出以下异常:
- UnsupportedOperationException - 如果集合无法添加元素。
- ClassCastException - 如果元素的类阻止方法将元素添加到集合中。
- NullPointerException - 如果集合为空
- IllegalArgumentException - 如果该方法的参数阻止将其添加到指定集合中
下面的程序说明了LinkedBlockingQueue类的drainTo(Collection<? super E> col,int maxElements)方法。
程序1:
以下程序具有存储Employee对象的LinkedBlockingQueue,其中有一个HashSet,该HashSet将从LinkedBlockingQueue存储所有Employee对象。因此,LinkedBlockingQueue的drainTo()用于将来自队列的一些员工传递到ArrayList中。因此,要传输的元素数量作为参数传递到方法中。
// Java程序,演示LinkedBlockingQueue的drainTo()方法。
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
// 创建具有职位和薪水属性的Employee对象
public class Employee {
public String name;
public String position;
public String salary;
Employee(String name, String position, String salary)
{
this.name = name;
this.position = position;
this.salary = salary;
}
@Override
public String toString()
{
return "Employee [name=" + name + ", "
+ "position=" + position + ", salary=" + salary + "]";
}
}
// 主方法
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.containsMethodExample();
}
public void containsMethodExample()
{
// 定义LinkedBlockingQueue的容量
int capacity = 10;
// 创建LinkedBlockingQueue对象
LinkedBlockingQueue<Employee> linkedQueue
= new LinkedBlockingQueue<Employee>(capacity);
// 创建HashSet对象以作为drainTo()方法的参数
HashSet<Employee> collection = new HashSet<Employee>();
// 向队列中添加Employee对象
Employee emp1 = new Employee("Sachin", "Analyst", "40000");
Employee emp2 = new Employee("Aman", "Developer", "69000");
Employee emp3 = new Employee("Kajal", "Accountant", "39000");
linkedQueue.add(emp1);
linkedQueue.add(emp2);
linkedQueue.add(emp3);
// 应用drainTo()方法之前打印Arraylist和queue
System.out.println("Before drainTo():");
System.out.println("No of Elements in Queue is " + linkedQueue.size());
System.out.println("Elements in Queue is as follows");
Iterator<Employee> listOfemp = linkedQueue.iterator();
while (listOfemp.hasNext())
System.out.println(listOfemp.next());
System.out.println("No of Elements in HashSet is " + collection.size());
System.out.println("Elements in HashSet is as follows:");
for (Employee emp : collection)
System.out.println(emp);
// 初始化通过drainTo()方法传递给collection的元素个数
int noOfElement = 2;
// 应用drainTo方法并将集合作为参数传递
int response = linkedQueue.drainTo(collection, noOfElement);
// 打印已传递的元素数量
System.out.println("\nNo of element passed: " + response);
// 应用drainTo()方法之后打印Arraylist和queue
System.out.println("\nAfter drainTo():");
System.out.println("No of Elements in Queue is " + linkedQueue.size());
System.out.println("Elements in Queue is as follows");
listOfemp = linkedQueue.iterator();
while (listOfemp.hasNext())
System.out.println(listOfemp.next());
System.out.println("No of Elements in HashSet is " + collection.size());
System.out.println("Elements in HashSet is as follows:");
for (Employee emp : collection)
System.out.println(emp);
}
}
极客教程