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() 方法:
方案一:
下面的程序有一个存储 Employee 对象的 LinkedBlockingQueue。有一个 ArrayList 将存储来自 LinkedBlockingQueue 的所有员工对象。所以 drainTo() 与 LinkedBlockingQueue 一起使用,将所有员工从队列传递到 ArrayList。
Java
// 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 linkedQueue
= new LinkedBlockingQueue(capacity);
// create a ArrayList to pass as parameter to drainTo()
ArrayList collection = new ArrayList();
// 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);
}
}
Java
// 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
linkedQueue = new LinkedBlockingQueue(capacityOfQueue);
// add elements to queue
linkedQueue.put(85461);
linkedQueue.put(44648);
linkedQueue.put(45654);
// create a collection with null
ArrayList add = null;
// try to drain null queue to collection
try {
linkedQueue.drainTo(add);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Java
// 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 linkedQueue
= new LinkedBlockingQueue(capacity);
// create a HashSet to pass as parameter to drainTo()
HashSet collection = new HashSet();
// 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 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():
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
// 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
linkedQueue = new LinkedBlockingQueue(capacityOfQueue);
// add elements to queue
linkedQueue.put(85461);
linkedQueue.put(44648);
linkedQueue.put(45654);
// create a collection with null
ArrayList 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 – 如果方法的参数阻止它被添加到指定的集合中
下面的程序说明了 LinkedBlockingQueue 类的 drainTo(Collection super E> col, int maxElements) 方法
方案一:
下面的程序有一个 LinkedBlockingQueue 存储 Employee 对象,还有一个 HashSet 将存储来自 LinkedBlockingQueue 的所有员工对象。所以 LinkedBlockingQueue 的 drainTo() 用于将一些员工从队列传递到 ArrayList。因此,没有要传输的元素作为参数传入方法中。
Java
// 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 linkedQueue
= new LinkedBlockingQueue(capacity);
// create a HashSet to pass as parameter to drainTo()
HashSet collection = new HashSet();
// 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 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 Java
- https://docs.oracle.com/javase/8/docs/api/java Java