Java中的ArrayBlockingQueuerainTo()方法
drainTo()ArrayBlockingQueue类的方法从此队列中删除所有可用的元素,并将它们添加到给定的集合中。它返回传送的元素数。
语法如下
int drainTo(Collection<? super E> c)
在此,c是集合,其中将传输此队列的元素。要使用ArrayBlockingQueue类,您需要导入以下包
import java.util.concurrent.ArrayBlockingQueue;
以下是实现drainTo()JavaArrayBlockingQueue类的方法的示例
示例
import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;
public class Demo {
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(10);
q.add(200);
q.add(310);
q.add(400);
q.add(450);
q.add(500);
q.add(550);
q.add(700);
System.out.println("ArrayBlockingQueue = " + q);
ArrayList arrList = new ArrayList();
q.drainTo(arrList);
System.out.println("Updated ArrayBlockingQueue = " + q);
System.out.println("Our collection = " + arrList);
}
}输出结果
ArrayBlockingQueue = [200, 310, 400, 450, 500, 550, 700] After drainTo Operation Updated ArrayBlockingQueue = [] Our collection = [200, 310, 400, 450, 500, 550, 700]
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短