2019独角兽企业重金招聘Python工程师标准>>>

分别通过三种方式创建Map对象,使用ExecutorService来并发运行5个线程,每个线程添加/获取500K个元素。

package cglib;

import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class StringNumber {
    
    
    public final static int THREAD_POOL_SIZE = 5;
    
    public static Map<String, Integer> crunchifyHashTableObject = null;
    public static Map<String, Integer> crunchifySynchronizedMapObject = null;
    public static Map<String, Integer> crunchifyConcurrentHashMapObject = null;
 
    public static void main(String[] args) throws InterruptedException {
 
        // Test with Hashtable Object
        crunchifyHashTableObject = new Hashtable<>();
        crunchifyPerformTest(crunchifyHashTableObject);
 
        // Test with synchronizedMap Object
        crunchifySynchronizedMapObject = Collections.synchronizedMap(new HashMap<String, Integer>());
        crunchifyPerformTest(crunchifySynchronizedMapObject);
 
        // Test with ConcurrentHashMap Object
        crunchifyConcurrentHashMapObject = new ConcurrentHashMap<>();
        crunchifyPerformTest(crunchifyConcurrentHashMapObject);
 
    }
 
    public static void crunchifyPerformTest(final Map<String, Integer> crunchifyThreads) throws InterruptedException {
 
        System.out.println("Test started for: " + crunchifyThreads.getClass());
        long averageTime = 0;
        for (int i = 0; i < 5; i++) {
 
            long startTime = System.nanoTime();
            ExecutorService crunchifyExServer = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
 
            for (int j = 0; j < THREAD_POOL_SIZE; j++) {
                crunchifyExServer.execute(new Runnable() {
                    @SuppressWarnings("unused")
                    @Override
                    public void run() {
 
                        for (int i = 0; i < 500000; i++) {
                            Integer crunchifyRandomNumber = (int) Math.ceil(Math.random() * 550000);
 
                            // Retrieve value. We are not using it anywhere
                            Integer crunchifyValue = crunchifyThreads.get(String.valueOf(crunchifyRandomNumber));
 
                            // Put value
                            crunchifyThreads.put(String.valueOf(crunchifyRandomNumber), crunchifyRandomNumber);
                        }
                    }
                });
            }
 
            // Make sure executor stops
            crunchifyExServer.shutdown();
 
            // Blocks until all tasks have completed execution after a shutdown request
            crunchifyExServer.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
 
            long entTime = System.nanoTime();
            long totalTime = (entTime - startTime) / 1000000L;
            averageTime += totalTime;
            System.out.println("2500K entried added/retrieved in " + totalTime + " ms");
        }
        System.out.println("For " + crunchifyThreads.getClass() + " the average time is " + averageTime / 5 + " ms\n");
    }
    
}

输出:

Test started for: class java.util.Hashtable
2500K entried added/retrieved in 3816 ms
2500K entried added/retrieved in 2916 ms
2500K entried added/retrieved in 3040 ms
2500K entried added/retrieved in 2819 ms
2500K entried added/retrieved in 2855 ms
For class java.util.Hashtable the average time is 3089 ms

Test started for: class java.util.Collections$SynchronizedMap
2500K entried added/retrieved in 3430 ms
2500K entried added/retrieved in 2775 ms
2500K entried added/retrieved in 2891 ms
2500K entried added/retrieved in 2710 ms
2500K entried added/retrieved in 2811 ms
For class java.util.Collections$SynchronizedMap the average time is 2923 ms

Test started for: class java.util.concurrent.ConcurrentHashMap
2500K entried added/retrieved in 1814 ms
2500K entried added/retrieved in 1135 ms
2500K entried added/retrieved in 1051 ms
2500K entried added/retrieved in 1008 ms
2500K entried added/retrieved in 1560 ms
For class java.util.concurrent.ConcurrentHashMap the average time is 1313 ms

转载于:https://my.oschina.net/u/2822116/blog/785674

比较SynchronizedMap、Hashtable和ConcurrentHashMap的效率相关推荐

  1. HashMap、HashTable、ConcurrentHashMap、HashSet区别 线程安全类

    HashMap专题:HashMap的实现原理--链表散列 HashTable专题:Hashtable数据存储结构-遍历规则,Hash类型的复杂度为啥都是O(1)-源码分析 Hash,Tree数据结构时 ...

  2. 面试必备:HashMap、Hashtable、ConcurrentHashMap的原理与区别

    本文转载自 夏雪冬日:https://www.cnblogs.com/heyonggang/p/9112731.html 在实际面试过程中出现集合 Map 的概率接近 100%,可见不背上个 Map ...

  3. Java集合——HashMap、HashTable以及ConCurrentHashMap异同比较

    转发:https://www.cnblogs.com/zx-bob-123/archive/2017/12/26/8118074.html 0. 前言 HashMap和HashTable的区别一种比较 ...

  4. 对比分析HashMap,HashTable,ConcurrentHashMap,LinkedHashMap,LURLinkedHashMap(一)

    前言: 这次写几篇 关于 HashMap,HashTable,ConcurrentHashMap,LinkedHashMap,LURLinkedHashMap 源码分析. 如果直接将他们源码,并不好理 ...

  5. 3、HashMap、HashTable和ConcurrentHashMap的区别?

    HashMap和HashTable的区别一种比较简单的回答是: (1)HashMap是非线程安全的,HashTable是线程安全的. (2)HashMap的键和值都允许有null存在,而HashTab ...

  6. HashMap、HashTable和ConcurrentHashMap的区别?

    HashMap和HashTable的区别一种比较简单的回答是: (1)HashMap是非线程安全的,HashTable是线程安全的. (2)HashMap的键和值都允许有null存在,而HashTab ...

  7. HashMap、HashTable和ConcurrentHashMap的区别

    HahMap.HashTable和ConcurrentHashMap的区别: HashMap HashTable ConcurrentHashMap null键 允许 不允许 不允许 null值 允许 ...

  8. Java经典面试题:HashMap和HashTable以及ConcurrentHashMap分析

    本文转载于:https://segmentfault.com/a/1190000038989327 前言: HashMap 应该算是 Java 后端工程师面试的必问题,因为其中的知识点太多,很适合用来 ...

  9. Java Main Differences between HashMap HashTable and ConcurrentHashMap

    转自这篇帖子:http://www.importnew.com/7010.html HashMap和Hashtable的比较是Java面试中的常见问题,用来考验程序员是否能够正确使用集合类以及是否可以 ...

最新文章

  1. HDU 2189 悼念512汶川大地震遇难同胞——来生一起走
  2. Jsoup使用DOM方法来遍历一个文档
  3. 不同专业领域使用计算机的例子,基于事例推理及遗传算法的应用-计算机应用技术专业论文.docx...
  4. Linux下常用安全策略设置方法
  5. 尚硅谷大数据开发Day01
  6. ASP.net 探针
  7. 二进制码和格雷码转换问题
  8. 聊天室后台 java php_PHP实现简单聊天室(附源码)
  9. 玩转HANA数据库的备份与恢复(2020 刘欣)
  10. mysql日期 select_MySQL_MySql日期查询语句详解,使用DATE_FORMAT方法SELECT * FROM `le - phpStudy...
  11. Java中对象属性的初始化顺序
  12. js 只准输入数字_javascript 限制只允许输入数字的几种方法
  13. c语言链表的插入 查询 删除
  14. Java各种加密方式集锦(AES,DES,RSA,DSA,MD5,SHA)
  15. 6、springboot-防止xxs攻击
  16. OP-TEE 简易驱动编写:启动TZPC与TZPCDEP
  17. 小程序下拉刷新 上拉加载等多
  18. 面试直通卡大放送,微软面试官带你揭秘面试潜规则!
  19. 如何离线安装Unity并添加离线帮助文档
  20. 网站上的SSL是什么意思?有什么作用?

热门文章

  1. 【SQL练习题】case when实现按要求排序
  2. 【异常:Could not resolve】react-native run-android
  3. Android开发中遇到的问题(四)——Android中WARNING: Application does not specify an API level requirement!的解决方法
  4. 计算机中丢失api-ms-win-crt-runtime-
  5. 解决Axure发布分享预览的3个方法
  6. POI实现大数据EXCLE导入导出,解决内存溢出问题
  7. 如何在C中纯粹编写iOS应用程序
  8. 检查字段是否包含字符串
  9. 函数式编程会取代GoF设计模式吗?
  10. 检索HTML元素的位置(X,Y)