hashmap是线程安全吗
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇内容主要讲解"hashmap是线程安全吗",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"hashmap是线程安全吗"吧!目前普遍都是使用jdk1.8 了
千家信息网最后更新 2026年11月03日hashmap是线程安全吗
本篇内容主要讲解"hashmap是线程安全吗",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"hashmap是线程安全吗"吧!
目前普遍都是使用jdk1.8 了。 其实要模拟jdk1.8的多线程不安全出来,其实很简单的。
复制一份hashmap的代码出来。接着 将 hashmap的容量固定为1 ,并且禁止扩容。
比如: 修改 resize代码。 第一次可以扩展,容量是1。第二次就不可以了。
final Node[] resize() { if (FirstResize.get() !=0){ System.out.println(">>>>不可以扩展>>>"); return table; }else{ FirstResize.addAndGet(1); }
put 方法里面,进行控制线程的调度,就可以模拟了
/** * Implements Map.put and related methods * * @param hash hash for key * @param key the key * @param value the value to put * @param onlyIfAbsent if true, don't change existing value * @param evict if false, the table is in creation mode. * @return previous value, or null if none */ final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node[] tab; Node p; int n, i; if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; if ((p = tab[i = (n - 1) & hash]) == null){ // 多线程并发,会发生 数据覆盖 if (Thread.currentThread().getName().equals("a")){ System.out.println("线程A休眠,暂停执行"); //模拟, 让其覆盖 其他线程 的值 try { Thread.sleep(5*1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("i >>>>"+i); System.out.println("插入:"+key +": "+value); tab[i] = newNode(hash, key, value, null); } else { Node e; K k; if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; else if (p instanceof TreeNode) e = ((TreeNode)p).putTreeVal(this, tab, hash, key, value); else { for (int binCount = 0; ; ++binCount) { if ((e = p.next) == null) { //同理也会 发生并发 put的时候被覆盖的情况 p.next = newNode(hash, key, value, null); if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st treeifyBin(tab, hash); break; } if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } if (e != null) { // existing mapping for key V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } ++modCount; if (++size > threshold) resize(); afterNodeInsertion(evict); return null; } 测试代码:
package com.interview.javabasic.myhashmap;import java.util.Map;import java.util.concurrent.atomic.AtomicInteger;public class TestThreadMap { private static HashMap mh=new HashMap(1); public static void main(String[] args) { HashMapThread thread0 = new HashMapThread("a",mh); HashMapThread thread1 = new HashMapThread("b",mh); thread0.start(); thread1.start(); try { Thread.sleep(15*1000); } catch (InterruptedException e) { e.printStackTrace(); } // 此时会发现 map 里面 只有 2,2 。 // mh.put(3,3); 被覆盖了 System.out.println(mh); }}class HashMapThread extends Thread { HashMap mh; private String namet; public HashMapThread(String name, HashMap mh) { super(name); this.namet=name; this.mh=mh; } @Override public void run() { if (namet.equals("b")){ try { Thread.sleep(2*1000); } catch (InterruptedException e) { e.printStackTrace(); } mh.put(3,3); }else{ mh.put(2,2); } }} 到此,相信大家对"hashmap是线程安全吗"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
线程
安全
代码
内容
容量
方法
学习
实用
更深
不安
兴趣
只有
实用性
实际
情况
操作简单
数据
时候
更多
朋友
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网与科技
科技金融是不是互联网金融
互联网科技公司华为
互联网科技巨头监管
科技互联网政务服务
互联网科技未来
互联网科技金融
上海挚品互联网科技
互联网与新科技
紫鲸互联网科技
电子科技大学互联网
隆华科技工业互联网
北京华百购互联网科技有限公司
来此购互联网科技有限公司
厦门比较好的科技互联网公司
彤创互联网科技有限公司
互联网科技和法律行业
科技赋能视角下互联网保险发展
互联网科技接地气
科技信息时代互联网发展
科技互联网
互联网科技海报
互联网科技保险
互联网扶贫科技
云电互联网科技
互联网和高科技公司
洛阳共同富裕互联网科技有限公司
广州互联网高科技取餐柜
北京军民互联网科技公司
马化腾互联网科技教学