package com.itheima.mymap;
import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;
public class MyConcurrenHashTabDemo {
public static void main(String[] args) throws InterruptedException {
ConcurrentHashMap<String, String> hm = new ConcurrentHashMap<>();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 25; i++) {
hm.put(i + "", i + "");
}
});
Thread t2 = new Thread(() -> {
for (int i = 25; i < 51; i++) {
hm.put(i + "", i + "");
}
});
t1.start();
t2.start();
System.out.println("--------------------------------------");
Thread.sleep(1000);
for (int i = 0; i < 51; i++) {
System.out.println(hm.get(i+""));
}
}
}
132.-并发工具类-ConcurrentHashMap基本使用__
最新推荐文章于 2025-12-08 09:18:56 发布
本文通过一个实例展示了如何在Java中使用ConcurrentHashMap进行多线程操作,两个线程分别向哈希表中插入数据,同时观察了并发情况下的数据一致性。
557

被折叠的 条评论
为什么被折叠?



