zoukankan      html  css  js  c++  java
  • 对比在ConcurrentLinkedQueue中查询队列中元素数量size()和isEmpty()的性能差距

    对比在ConcurrentLinkedQueue中查询队列中元素数量size()和isEmpty()的性能差距

    package com.dwz.concurrent;
    
    import java.util.concurrent.ConcurrentLinkedQueue;
    
    /**
     *	并发集合
     *	对比在ConcurrentLinkedQueue中查询队列中元素数量size()和isEmpty()的性能差距
     */
    public class ConcurrentLinkedQueueExample {
    	
    	public static void useSize() {
    		final ConcurrentLinkedQueue<Long> queue = new ConcurrentLinkedQueue<>();
    		
    		for(int i = 0; i < 100000; i++) {
    			queue.offer(System.nanoTime());
    		}
    		
    		System.out.println("========= offer done ==========");
    		long startTime = System.currentTimeMillis();
    		while(queue.size() > 0) {
    			queue.poll();
    		}
    		
    		System.out.println("========= poll done ==========");
    		System.out.println("useSize: " + (System.currentTimeMillis() - startTime));
    	}
    	
    	public static void useIsEmpty() {
    		final ConcurrentLinkedQueue<Long> queue = new ConcurrentLinkedQueue<>();
    		
    		for(int i = 0; i < 100000; i++) {
    			queue.offer(System.nanoTime());
    		}
    		
    		System.out.println("========= offer done ==========");
    		long startTime = System.currentTimeMillis();
    		while(!queue.isEmpty()) {
    			queue.poll();
    		}
    		
    		System.out.println("========= poll done ==========");
    		System.out.println("useIsEmpty: " + (System.currentTimeMillis() - startTime));
    	}
    	
    	public static void main(String[] args) {
    		useSize();
    		useIsEmpty();
    	}
    }
    

      代码运行结果

    ========= offer done ==========
    ========= poll done ==========
    useSize: 12340
    ========= offer done ==========
    ========= poll done ==========
    useIsEmpty: 3
    

      测试结果:

    ConcurrentLinkedQueue的isEmpty()比size()效率高很多
  • 相关阅读:
    window下安装两个mysql服务
    Linux 下 FastDFS v5.08 分布式文件系统的安装
    linux 下 php 安装 ZeroMQ 扩展
    win 下 nginx 与 php的配置
    Navicat Premium11连接Oracle出现ORA-28547:connection to server failed
    dedecms的自定义模块
    php 的多进程实践
    php多进程 防止出现僵尸进程
    php Pthread 多线程 (一) 基本介绍
    php 使用PHPExcel 导出数据为Excel
  • 原文地址:https://www.cnblogs.com/zheaven/p/13801290.html
Copyright © 2011-2022 走看看