---
title: "java 并发环境下使用ConcurrentHashMap"
author: "vanadiumlin"
date: "2011-10-18T16:41:42Z"
tags: []
categories: ["编程语言"]
slug: "java-concurrenthashmap"
draft: false
lastmod: "2011-11-24T19:29:45Z"
id: 1199416
url: "https://vanadiumlin.iteye.com/blog/1199416"
---

一般如果是需要线程安全的话，Map使用ConcurrentHashMap，List,Vector使用CopyOnWriteArrayList

Set 使用CopyOnWriteArraySet（别问我为什么，大家都这么用）

而且性能比 Collections.synchronizedMap(new HashMap&lt;String, Long&gt;()); 的封装方式要好

http://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/index.html?ca=drs

相比较于 HashTable 和由同步包装器包装的 HashMap每次只能有一个线程执行读或写操作，ConcurrentHashMap 在并发访问性能上有了质的提高。在理想状态下，ConcurrentHashMap 可以支持 16 个线程执行并发写操作（如果并发级别设置为 16），及任意数量线程的读操作。

而队列在并发的情况下应该使用ConcurrentQueue