`
weiqiang.yang
  • 浏览: 155122 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

[Java]关于HashMap的多线程不安全

阅读更多
    HashMap不是多线程安全的,这个貌似大家都知道,但是这是不是意味着,只要在多线程环境下使用到HashMap,那就得套上syncronized呢?
  
引用
Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap  class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.


    HashMap是基于Hash Table的一种Map实现,允许null values和null key(它跟HashTable的区别在于它是非同步的,而且允许null key和null values),HashMap不保证Map的顺序,甚至不能保证Map的顺序在运行过程中保持不变。

   
引用
Note that this implementation is not synchronized. If multiple threads access this map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:


 Map m = Collections.synchronizedMap(new HashMap(...));


    这种实现不是同步的,如果多个线程同时访问这个Map,而且至少一个线程对Map进行结构性的修改(增加,删除操作,update不算),那么它必须在外部进行同步,通常这种同步是通过包含这个Map的外部对象来实现的,如果该外部对象不存在,那么这个Map需要通过Collections.synchronizedMap来保证同步(最好在创建的时候)。

  
引用
The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

    Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.


   话说,没看懂~~
关于fail-fast,中文意思是快速失败,没搜到官方的解释(搜索技术不好?),找到这个问答
http://www.jguru.com/faq/view.jsp?EID=221988

引用
An iterator is considered fail-fast  if it throws a ConcurrentModificationException under either of the following two conditions:
   1. In multithreaded processing: if one thread is trying to modify a Collection while another thread is iterating over it.
   2. In single-threaded or in multithreaded processing: if after the creation of the Iterator, the container is modified at any time by any method other than the Iterator's own remove or add methods.

  
来自ibm developerworks上对java.util.concurrent包的说明片段:
      java.util 包中的集合类都返回 fail-fast 迭代器,这意味着它们假设线程在集合内容中进行迭代时,集合不会更改它的内容。如果 fail-fast 迭代器检测到在迭代过程中进行了更改操作,那么它会抛出 ConcurrentModificationException ,这是不可控异常。
      在迭代过程中不更改集合的要求通常会对许多并发应用程序造成不便。相反,比较好的是它允许并发修改并确保迭代器只要进行合理操作,就可以提供集合的一致视图,如 java.util.concurrent 集合类中的迭代器所做的那样。
     java.util.concurrent 集合返回的迭代器称为弱一致的(weakly consistent)迭代器。对于这些类,如果元素自从迭代开始已经删除,且尚未由 next() 方法返回,那么它将不返回到调用者。如果元素自迭代开始已经添加,那么它可能返回调用者,也可能不返回。在一次迭代中,无论如何更改底层集合,元素不会被返回两次。

   算了,如果多线程用到HashMap,如果不能保证是只读的,还是给它套个syncronized好了~~
   丫的我还真没钻研精神~~
分享到:
评论
1 楼 szh_521 2014-09-28  
丫的我还真没钻研精神~~
   

相关推荐

    关于如何解决HashMap线程安全问题的介绍

    HashMap为什么是线程不安全的?如何解决HashMap的线程不安全问题?

    java的hashMap多线程并发情况下扩容产生的死锁问题解决.docx

    这就有可能导致A线程和B线程同时对一个数组扩容,A线程扩容后替换掉老数组,这时B线程使用的数组实际上是A线程扩容后的数组,就会产生线程安全问题。 死锁原因 比如,当前集合数组长度为2,已经有两个元素被放在了...

    hashmap-thread-test:测试 Java HashMap 是否是线程安全的

    哈希映射线程测试使用 Maven 构建和运行 mvn exec:java

    Java集合多线程安全.docx

    Java集合多线程安全 线程安全与不安全集合 线程不安全集合: ArrayList LinkedList HashMap HashSet TreeMap TreeSet StringBulider 线程安全集合: Vector HashTable Properties 集合线程安全...

    java中HashMap详解

    非线程安全:如果多个线程同时访问同一个HashMap实例,可能会导致数据不一致的问题。因此,在使用HashMap时需要进行同步处理或者使用线程安全的HashMap实现类。 动态扩容:当HashMap中的元素数量超过了容量(默认为...

    Java多线程编程的线程安全性.docx

    Java标准库中的一些类如ArrayList、HashMap和SimpleDateFormat,都是非线程安全的,在多线程环境下直接使用它们可能导致一些非预期的结果,甚至是一些灾难性的结果。一般来说,Java标准库中的类在其API文档中会说明...

    Java魔法解密:揭秘HashMap底层机制.pptx.pptx

    HashMap的存储结构 HashMap内部采用数组和链表的方式存储数据,每个元素都包含...HashMap通过synchronized关键字实现线程安全,确保多线程环境下的数据一致性和并发访问的安全性,避免潜在的竞争条件和数据不一致问题。

    concurrent 多线程 教材

    00 IBM developerWorks 中国 : Java 多线程与并发编程专题 02 Java 程序中的多线程 03 编写多线程的 Java 应用程序 04 如果我是国王:关于解决 Java编程语言线程问题的建议 (2) 05 构建Java并发模型框架 (2) 06...

    java7hashmap源码-network_program:学习java网络编程

    多线程 @since2020.11.26 internet地址 @since2020.11.27 URL 和 URI 类 @since2020.11.28 HTTP @since 2020.11.28 URLConnection @since 2020.11.29 Socket ServerSocket 学习 @since 2020.12.01 非阻塞I/O @since ...

    java7hashmap源码-lf-study-java-multithreading:华东师范及马士兵多线程,项目主要介绍java多线程以及

    java多线程和多进程 以下内容包含:华东师范大学的多线程讲解 及 马士兵多线程讲解 马士兵多线程讲解迁移位置:仓库 DOCRecord\ResteasyComplexDemo\src\pers\lishbo\timetask 1.多进程: 1.当前的操作系统都是多...

    Java面试全方位复习攻略,详解集合框架,hashmap底层原理,多线程,消息队列,radis,spring-boot ,Spring-cloud技术,等等

    Java面试全方位复习攻略,详解集合框架,hashmap底层原理,多线程,消息队列,radis,spring-boot ,Spring-cloud技术,等等

    HashMap死循环原因分析.docx

    HashMap是Java中常用的数据结构,但是它在多线程环境下可能会出现死循环的问题,使CPU占用率达到100%。这种情况是如何产生的呢?下面我们将从源码中一步一步地分析这种情况是如何产生的。 首先,我们需要了解...

    java7hashmap源码-Concurrency:这是用来学习java多线程的

    却会影响到多线程并发执行的正确性 * ############## 5.安全发布对象 发布对象->对一个对象能够被当前范围之外的代码所使用 对象逸出->一种错误的发布.当一个对象还没有构造完成时.就使他被其他线程所见 安全的发布...

    125条常见的java面试笔试题大汇总

    HashMap和Hashtable的区别...不是,在多个线程访问Hashtable时,不需要自己为它的方法实 现同步,而HashMap 就必须为之提供外同步。 Hashtable和HashMap采用的hash/rehash算法都大概一样,所 以性能不会有很大的差异。

    java7hashmap源码-java-note:笔记

    内存操作、线程切换消耗、多路复用 应用4 HyperLogLog 应用5 布隆过滤器 应用6 简单限流 应用7 漏斗限流 原理3 持久化(AOF 和 RDB) 集群、哨兵 集群模式下的发布订阅模式失效 6节点,三主(哨兵)三备 MySQL ...

    java7hashmap源码-springcloud-one:第一个自搭建微服务

    java7 hashmap源码 ...HashMap因为非线程安全假设两个线程下同时插入数据 A线程放入key A B C B线程也同时放入 A B C 遇到容量不足的时候需要新建一个更大尺寸的HASH表,会出现 如 a->b ,b->a 形成环形

    java程序员面试题

    最大的不同是,Hashtable的方法是Synchronize的,而HashMap不是,在多个线程访问Hashtable时,不需要自己为它的方法实现同步,而HashMap 就必须为之提供外同步。 Hashtable和HashMap采用的hash/rehash算法都大概...

    HashMap底层实现原理HashMap与HashTable区别HashMap与HashSet区别.docx

    HashMap底层实现原理HashMap与HashTable区别HashMap与HashSet区别。...因此,在多线程环境下,HashTable比HashMap更安全,但是性能可能较差。此外,HashMap允许将null作为键和值使用,而HashTable不允许。

    Java中的HashMap浅析

    在Java的集合框架中,HashSet,HashMap是用的比较多的一种,顺序结构的ArrayList、LinkedList这种也比较多,而像那几个线程同步的容器用的比较少,像Vector和HashTable,因为这两个线程同步的容器已经不被JDK推荐...

    Java 面试资源(基础 多线程)

    包含四个文件:java 基础上 基础下,多线程和集合。 Java集合框架的基础接口有哪些 Collection 和 Collections 有什么区别 List、Set、Map是否继承自Collection接口 Collections.sort排序内部原理 HashMap 的实现...

Global site tag (gtag.js) - Google Analytics