java 7 及之前的 hashmap 在多线程环境下是不安全的,会引发数据覆盖、死循环或无限循环等严重问题。处理并发的标准做法是直接使用线程安全的 concurrenthashmap,它通过分段锁(jdk 1.7)或 cas + synchronized(jdk 1.8+)实现高并发下的安全访问。

为什么 HashMap 在多线程下不安全
HashMap 不是线程安全的,因为它的内部操作如扩容、插入和删除都没有同步机制。当多个线程同时执行 put 操作时,如果发生哈希冲突且需要扩容,会导致链表成环,进而使后续遍历或插入操作陷入死循环。此外,非原子性的读写操作还可能导致数据丢失或读到脏数据。
import java.util.HashMap;
import java.util.Map;
public class UnsafeHashMapDemo {
private static Map<string string> map = new HashMap();
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
for (int i = 0; i {
for (int i = 0; i <p>上述代码中,两个线程同时向 <code>map</code> 写入数据,最终输出的 size 往往小于预期值,这是因为部分写入被覆盖或丢失。</p>
<h2>如何使用 ConcurrentHashMap 替代</h2>
<p><code>ConcurrentHashMap</code> 是 Java 并发包中推荐的线程安全 Map 实现。它在 JDK 1.8 中采用了 Node 数组 + 链表/红黑树的结构,并使用 synchronized 关键字锁定桶头节点,从而在保证线程安全的同时提高了并发性能。与 <code>Hashtable</code> 或 <code>Collections.synchronizedMap</code> 不同,<code>ConcurrentHashMap</code> 允许并发读取且无需加锁,写操作也仅锁定相关桶。</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill12680" title="JavaScript 开发专家"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20dee630206.jpg" alt="JavaScript 开发专家" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill12680" title="JavaScript 开发专家" class="overflowclass">JavaScript 开发专家</a>
<p class="overflowclass">掌握包含 ES6+、异步模式和 Node.js API 的现代 JavaScript。处理 Promise、事件循环和浏览器/Node 兼容性。在 JavaSc</p>
</div>
<a rel="nofollow" href="/xiazai/skill12680" title="JavaScript 开发专家" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
<pre class="brush:php;toolbar:false;">import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class SafeConcurrentHashMapDemo {
private static ConcurrentMap<string string> map = new ConcurrentHashMap();
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
for (int i = 0; i {
for (int i = 0; i <p>使用 <code>ConcurrentHashMap</code> 后,即使在高并发场景下,也能保证数据的正确性和一致性,且性能优于传统的同步集合类。</p>
<h2>ConcurrentHashMap 的 putIfAbsent 行为分析</h2>
<p>在并发场景中,经常需要“如果键不存在则插入”的操作。<code>ConcurrentHashMap</code> 提供了 <code>putIfAbsent</code> 方法,该方法是一个原子操作,避免了先检查再插入带来的竞态条件。例如,当多个线程尝试插入同一个键时,只有第一个成功的线程会写入值,其他线程将返回已存在的值,而不会覆盖。</p>
<pre class="brush:php;toolbar:false;">import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class PutIfAbsentDemo {
private static ConcurrentMap<string integer> map = new ConcurrentHashMap();
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
Integer result = map.putIfAbsent("counter", 1);
System.out.println("Thread-1 putIfAbsent returned: " + result);
});
Thread t2 = new Thread(() -> {
try { Thread.sleep(10); } catch (InterruptedException e) {}
Integer result = map.putIfAbsent("counter", 2);
System.out.println("Thread-2 putIfAbsent returned: " + result);
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Final value: " + map.get("counter"));
}
}</string>
运行该程序,Thread-1 成功插入 1 并返回 null,Thread-2 发现键已存在,返回 1 而不覆盖。最终值为 1,确保了唯一性。
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南










