BlockingQueue的drainTo(Collection col)方法从此LinkedBlocking队列中删除所有可用元素,并将它们添加到作为参数传递的给定集合中。
注意:BlockingQueue的drainTo()方法已从Java中的Queue类继承。
rainingTo(Collection super E> col)
BlockingQueue接口的drainTo(Collection super E> col)方法从此队列中删除所有元素,并将它们添加到给定的collection col中。与重复轮询此队列相比,这是一种更有效的方法。
尝试从队列中将元素添加到集合c时,也可能会遇到失败,并且由于该失败,当引发关联的异常时,元素会在两个集合之间分配。如果尝试使用队列将drainTo()自身进行队列,则将抛出IllegalArgumentException。如果在操作进行过程中修改了指定的集合,则此操作的行为是不确定的。因此,使用这种方法时,需要注意这种情况以克服异常。
用法:
public int drainTo(Collection<? super E> col)
参数:此方法接受一个参数col,该参数表示要从LinkedBlockingQueue传输元素的集合。
返回值:此方法返回从队列中耗尽到集合的元素数。
异常:此方法引发以下异常:
- UnsupportedOperationException–如果collection无法添加元素。
- ClassCastException–如果元素类停止将元素添加到集合的方法。
- NullPointerException –如果集合为空
- IllegalArgumentException–如果方法的参数阻止将其添加到指定的集合中
以下示例程序旨在说明BlockingQueue类的drainTo()方法:
示例1:
// Java Program Demonstrate drainTo(Collection c)
// method of BlockingQueue.
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
// create a Employee Object with
// position and salary as an attribute
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 + "]";
}
}
// Main Method
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.containsMethodExample();
}
public void containsMethodExample()
{
// define capacity of BlockingQueue
int capacity = 50;
// create object of BlockingQueue
BlockingQueue<Employee> BQ
= new LinkedBlockingQueue<Employee>(capacity);
// create a ArrayList to pass as parameter to drainTo()
ArrayList<Employee> collection = new ArrayList<Employee>();
// add Employee object to queue
Employee emp1 = new Employee("Aman", "Analyst", "24000");
Employee emp2 = new Employee("Sachin", "Developer", "39000");
BQ.add(emp1);
BQ.add(emp2);
// printing Arraylist and queue
System.out.println("Before drainTo():");
System.out.println("BlockingQueue : \n"
+ BQ.toString());
System.out.println("ArrayList : \n"
+ collection);
// Apply drainTo method and pass collection as parameter
int response = BQ.drainTo(collection);
// print no of element passed
System.out.println("\nNo of element passed: " + response);
// printing Arraylist and queue after applying drainTo() method
System.out.println("\nAfter drainTo():");
System.out.println("BlockingQueue : \n"
+ BQ.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:
// Java Program Demonstrate
// drainTo(Collection C)
// method of BlockingQueue.
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of BlockingQueue
int capacityOfQueue = 4;
// create object of BlockingQueue
BlockingQueue<Integer>
BQ = new LinkedBlockingQueue<Integer>(capacityOfQueue);
// add elements to queue
BQ.put(85461);
BQ.put(44648);
BQ.put(45654);
// create a collection with null
ArrayList<Integer> add = null;
// try to drain null queue to collection
try {
BQ.drainTo(add);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Exception: java.lang.NullPointerException
rainageTo(Collection super E> col,int maxElements)
用于将固定数量的元素(它在drainTo()中作为整数传递)传递给collection(也作为参数传递给方法)的rainingTo(Collection super E> col,int maxElements)。传输元素后,BlockingQueue仅具有那些未传输到集合的元素。此函数与上述函数相同,但在转移固定元素数量方面存在一些限制。
用法:
public int drainTo(Collection<? super E> col, int maxElements)
参数:该方法接受两个参数:
- col–它表示要从BlockingQueue传输元素的集合。
- maxElements–这是整数类型,是指要传输到集合的最大元素数。
返回值:该方法返回从队列中耗尽到集合的元素数。
异常:此方法引发以下异常:
- UnsupportedOperationException–如果collection无法添加元素。
- ClassCastException-if元素类的stop方法将元素添加到集合中。
- NullPointerException –如果集合为空
- IllegalArgumentException–如果方法的参数阻止将其添加到指定的集合中
以下示例程序旨在说明BlockingQueue类的raintTo(Collection super E> col,int maxElements)方法
示例1:
下面的程序有一个BlockingQueue,它存储Employee对象,还有一个HashSet,它将存储BlockingQueue中的所有员工对象。因此,使用BlockingQueue的drainTo()将一些Employee从队列传递到ArrayList。因此,没有要传递的元素作为参数传递给方法。
// Java program to demonstrate drainTo()
// method of BlockingQueue.
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
// create a Employee Object with
// position and salary as attribute
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 + "]";
}
}
// Main Method
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.containsMethodExample();
}
public void containsMethodExample()
{
// define capacity of BlockingQueue
int capacity = 10;
// create object of BlockingQueue
BlockingQueue<Employee> BQ
= new LinkedBlockingQueue<Employee>(capacity);
// create a HashSet to pass as parameter to drainTo()
HashSet<Employee> collection = new HashSet<Employee>();
// add Employee object to queue
Employee emp1 = new Employee("Sachin", "Analyst", "40000");
Employee emp2 = new Employee("Aman", "Developer", "69000");
Employee emp3 = new Employee("Kajal", "Accountant", "39000");
BQ.add(emp1);
BQ.add(emp2);
BQ.add(emp3);
// printing Arraylist and queue before applying drainTo() method
System.out.println("Before drainTo():");
System.out.println("No of Elements in Queue is " + BQ.size());
System.out.println("Elements in Queue is as follows");
Iterator<Employee> listOfemp = BQ.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);
// Initialize no of element passed to collection
// using drainTo() method
int noOfElement = 2;
// Apply drainTo method and pass collection as parameter
int response = BQ.drainTo(collection, noOfElement);
// print no of element passed
System.out.println("\nNo of element passed: " + response);
// printing Arraylist and queue after applying drainTo() method
System.out.println("\nAfter drainTo():");
System.out.println("No of Elements in Queue is " + BQ.size());
System.out.println("Elements in Queue is as follows");
listOfemp = BQ.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);
}
}
Before drainTo(): No of Elements in Queue is 3 Elements in Queue is as follows Employee [name=Sachin, position=Analyst, salary=40000] Employee [name=Aman, position=Developer, salary=69000] Employee [name=Kajal, position=Accountant, salary=39000] No of Elements in HashSet is 0 Elements in HashSet is as follows: No of element passed: 2 After drainTo(): No of Elements in Queue is 1 Elements in Queue is as follows Employee [name=Kajal, position=Accountant, salary=39000] No of Elements in HashSet is 2 Elements in HashSet is as follows: Employee [name=Sachin, position=Analyst, salary=40000] Employee [name=Aman, position=Developer, salary=69000]
参考:
- https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#drainTo(java.util.Collection)
- https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#drainTo(java.util.Collection, %20int)
相关用法
- Java BlockingQueue contains()用法及代码示例
- Java BlockingQueue take()用法及代码示例
- Java BlockingQueue put()用法及代码示例
- Java BlockingQueue remainingCapacity()用法及代码示例
- Java BlockingQueue poll()用法及代码示例
- Java BlockingQueue offer()用法及代码示例
- Java BlockingQueue remove()用法及代码示例
- Java DelayQueue drainTo()用法及代码示例
- Java BlockingQueue add()用法及代码示例
- Java LinkedTransferQueue drainTo()用法及代码示例
- Java LinkedBlockingDeque drainTo()用法及代码示例
- Java PriorityBlockingQueue drainTo()用法及代码示例
- Java LinkedBlockingQueue drainTo()用法及代码示例
- Java ArrayBlockingQueue drainTo()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 BlockingQueue drainTo() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。