Map的一些实现类有及其特性

线程安全 特性

Hashtable

Key不能为null

HashMap

读写效率最高,但在Java6多线程环境下使用不当可能陷入死循环,进而导致CPU使用率过高(原理可参见:http://coolshell.cn/articles/9606.html)

Collections.synchronizedMap

Collections.SynchronizedMap在Map所有方法基础上加锁,效率与HashTable相当

ConcurrentHashMap

采用分段锁,get一般不加锁,put分段锁,key/value不能为null,效率仅次于HashMap

以下代码测试各类的读写效率:

package com.concurrent.test;

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; /**
* 测试map
*/
public class ThreadMapTest { public static void main(String[] args) throws InterruptedException {
Map<Integer, Integer> hashtable = new Hashtable<>();
Map<Integer, Integer> hashmap = new HashMap<>();
Map<Integer, Integer> synchronizedHashMap = Collections.synchronizedMap(new HashMap<Integer, Integer>());
Map<Integer, Integer> concurrentHashMap = new ConcurrentHashMap<>(); test(hashtable);
test(hashmap);
test(synchronizedHashMap);
test(concurrentHashMap);
} private static void test(Map<Integer, Integer> map) throws InterruptedException {
int testTimes = 5;
long totalTimeMillis = 0;
for (int k = 0; k < testTimes; k++) {
totalTimeMillis += costTimeMillis(map);
}
System.out.println("Test " + map.getClass() + " average time " + (totalTimeMillis / testTimes));
} private static long costTimeMillis(final Map<Integer, Integer> map) throws InterruptedException {
int count = 5;
ExecutorService executorService = Executors.newFixedThreadPool(count);
long startMillis = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
executorService.execute(new Runnable() { @Override
public void run() {
for (int j = 0; j < 50000; j++) {
map.put(0, 0);
map.get(0);
}
}
}); }
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
return System.currentTimeMillis() - startMillis;
} }

输出结果如下:

Test class java.util.Hashtable average time 104
Test class java.util.HashMap average time 31
Test class java.util.Collections$SynchronizedMap average time 79
Test class java.util.concurrent.ConcurrentHashMap average time 66

【Java多线程系列六】Map实现类的更多相关文章

  1. Java多线程系列六——Map实现类

    参考资料: https://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-syn ...

  2. java多线程系列(六)---线程池原理及其使用

    线程池 前言:如有不正确的地方,还望指正. 目录 认识cpu.核心与线程 java多线程系列(一)之java多线程技能 java多线程系列(二)之对象变量的并发访问 java多线程系列(三)之等待通知 ...

  3. java多线程系列六、线程池

    一. 线程池简介 1. 线程池的概念: 线程池就是首先创建一些线程,它们的集合称为线程池. 2. 使用线程池的好处 a) 降低资源的消耗.使用线程池不用频繁的创建线程和销毁线程 b) 提高响应速度,任 ...

  4. Java多线程系列——线程阻塞工具类LockSupport

    简述 LockSupport 是一个非常方便实用的线程阻塞工具,它可以在线程内任意位置让线程阻塞. 和 Thread.suspend()相比,它弥补了由于 resume()在前发生,导致线程无法继续执 ...

  5. 【Java多线程系列五】列表类

    一些列表类及其特性  类 线程安全 Iterator 特性 说明 Vector 是 fail-fast 内部方法用synchronized修饰,因此执行效率较低 1. 线程安全的列表类并不意味着调用它 ...

  6. 【Java多线程系列二】Thread类的方法

    Thread实现Runnable接口并实现了大量实用的方法. /* * 此方法释放CPU,但并不释放已获得的锁,其它就绪的线程将可能得到执行机会,它自己也有可能再次得到执行机会 */ public s ...

  7. (Java多线程系列六)join()的用法和线程的优先级

    join()的用法和线程的优先级 1.join()的用法 join()作用就是让其他线程处于等待状态 先看一个需求:创建一个线程,子线程执行完毕后,主线程才能执行 public class JoinT ...

  8. Java多线程系列——从菜鸟到入门

    持续更新系列. 参考自Java多线程系列目录(共43篇).<Java并发编程实战>.<实战Java高并发程序设计>.<Java并发编程的艺术>. 基础 Java多线 ...

  9. Java多线程系列--“JUC锁”08之 共享锁和ReentrantReadWriteLock

    概要 Java的JUC(java.util.concurrent)包中的锁包括"独占锁"和"共享锁".在“Java多线程系列--“JUC锁”02之 互斥锁Ree ...

随机推荐

  1. Python sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings......

    完整的错误信息如下: You must not use 8-bit bytestrings unless you use a text _factory that can interpret 8-bi ...

  2. (转)Git 提交的正确姿势:Commit message 编写指南

    Git 每次提交代码,都要写 Commit message(提交说明),否则就不允许提交. $ git commit -m "hello world" 上面代码的-m参数,就是用来 ...

  3. 多条件异步搜索+分页(PHP、 AJAX、ThinkPHP)

    项目中遇到的多条件异步查询及数据分页问题,做了数次尝试,最终虽目的达到,略有繁琐,希望能有更好的处理方式 基于 tp框架 1.html页面代码 <div class="h_cityNa ...

  4. gitlab+gitlab-ci+docker自动化部署

    导言 本次测试用的是gitlab-ci,单纯与gitlab搭配而言,gitlab-ci较jenkins更加一体,顺畅. 主机1:192.168.100.151 gitlab 主机2:192.168.1 ...

  5. document.readyState和document.DOMContentLoaded判断DOM的加载完成

    document.readyState:判断文档是否加载完成.firefox不支持. 这个属性是只读的,传回值有以下的可能: 0-UNINITIALIZED:XML 对象被产生,但没有任何文件被加载. ...

  6. 如何深入理解Java泛型

    一.泛型的作用与定义 1.1泛型的作用 使用泛型能写出更加灵活通用的代码泛型的设计主要参照了C++的模板,旨在能让人写出更加通用化,更加灵活的代码.模板/泛型代码,就好像做雕塑时的模板,有了模板,需要 ...

  7. Spring Boot,Spring Data JPA多数据源支持配置

    1 配置文件 wisely.primary.datasource.driverClassName=oracle.jdbc.OracleDriver wisely.primary.datasource. ...

  8. ionic3.0 中带顶部导航的下拉刷新列表的实现

    1.最终实现效果 2.html代码布局: 3.css样式控制(注:下面这两个css类名需在浏览器解析后才可看到)

  9. 杭电多校第六场-J-Ridiculous Netizens

    Problem Description Mr. Bread has a tree T with n vertices, labeled by 1,2,…,n. Each vertex of the t ...

  10. Eclipse中发布Maven管理的Web项目时找不到类的问题根源和解决办法(转)

    转自:http://blog.csdn.net/lvguanming/article/details/37812579?locationNum=12 写在前面的话 现在是越来越太原讨厌Eclipse这 ...