import java.util.concurrent.ConcurrentLinkedQueue;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ConcurrentLinkedQueueTest1 { public static void main(String[] args) throws InterruptedException { int peopleNum = 10000;//吃饭人数 int tableNUm = 10;//饭桌数量 ConcurrentLinkedQueuequeue = new ConcurrentLinkedQueue<>();//队列 CountDownLatch count = new CountDownLatch(tableNUm);//计数器 //将吃饭人数放入队列(吃饭的人排队) for(int i = 0;i queue; private CountDownLatch count; public Dinner(String name, ConcurrentLinkedQueue queue, CountDownLatch count) { this.name = name; this.queue = queue; this.count = count; } @Override public void run() { //通过两种方式比较耗时 while(queue.size()>0){ // while(!queue.isEmpty()){ System.out.println("【"+queue.poll() +"】---已吃完...,饭桌编号:"+name); } count.countDown(); } }}
通过上面的测试,得出结论:size()方法比isEmpty()耗时更长,原因是size() 是要遍历一遍集合的.