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 – 如果集合是空的。
- IllegalArgumentException – 如果方法的参数阻止它被添加到指定的集合中。
下面的程序说明了LinkedBlockingQueue类的drainTo()方法:
程序1:
下面的程序有一个LinkedBlockingQueue,用来存储Employee对象。有一个ArrayList,它将存储所有来自LinkedBlockingQueue的雇员对象。因此,drainTo()被用于LinkedBlockingQueue,将所有的雇员从队列中传递到ArrayList。
// Java Program Demonstrate drainTo(Collection c)
// method of LinkedBlockingQueue.
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;
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 LinkedBlockingQueue
int capacity = 50;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<Employee> linkedQueue
= 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");
linkedQueue.add(emp1);
linkedQueue.add(emp2);
// printing Arraylist and queue
System.out.println("Before drainTo():");
System.out.println("LinkedBlockingQueue : \n"
+ linkedQueue.toString());
System.out.println("ArrayList : \n"
+ collection);
// Apply drainTo method and pass collection as parameter
int response = linkedQueue.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("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 Program Demonstrate
// drainTo(Collection C)
// method of LinkedBlockingQueue.
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 4;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<Integer>
linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue);
// add elements to queue
linkedQueue.put(85461);
linkedQueue.put(44648);
linkedQueue.put(45654);
// create a collection with null
ArrayList<Integer> add = null;
// try to drain null queue to collection
try {
linkedQueue.drainTo(add);
}
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 – 如果方法的参数阻止它被添加到指定的集合中。
下面的程序说明了LinkedBlockingQueen类的drainTo(Collection<? super E> col, int maxElements)方法
程序1:
下面的程序有一个存储Employee对象的LinkedBlockingQueen,其中有一个HashSet,将存储LinkedBlockingQueen中的所有雇员对象。因此,LinkedBlockingQueue的drainTo()被用来从队列中传递一些雇员到ArrayList。因此,要转移的元素数量作为参数被传递到方法中。
// Java program to demonstrate drainTo()
// method of LinkedBlockingQueue.
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
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 LinkedBlockingQueue
int capacity = 10;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<Employee> linkedQueue
= 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");
linkedQueue.add(emp1);
linkedQueue.add(emp2);
linkedQueue.add(emp3);
// printing Arraylist and queue before applying drainTo() method
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);
// Initialize no of element passed to collection
// using drainTo() method
int noOfElement = 2;
// Apply drainTo method and pass collection as parameter
int response = linkedQueue.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 " + 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);
}
}
输出
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/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#drainTo-java.util.Collection-
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#drainTo-java.util.Collection-int-
极客教程