【发布时间】:2022-11-03 02:53:36
【问题描述】:
我正在尝试使用多个线程测试对 ConcurrentHashMap 的并发放置,但处理后哈希图的大小不是我所期望的。
我有以下代码将 1000 个条目插入 ConcurrentHashMap:
@Test
public void testThreadSafetyConcurrentHashMap() {
Map<Integer, Integer> map = new ConcurrentHashMap<>();
Runnable runnable = () -> {
for (int i = 0; i < 1000; i++) {
map.put(i, i);
}
};
ExecutorService executorService = Executors.newFixedThreadPool(4);
for (int i = 0; i < 4; i++) {
executorService.submit(runnable);
}
System.out.println(map.size());
}
我期待map.size() 电话有 1000 件物品,但我不是每次都得到这个。
有人可以告诉我问题是什么吗?我以为4个线程同时放入1000个项目,最终会导致总共1000个项目?
【问题讨论】:
-
打印地图大小时是否所有可运行文件都已完成?
-
启动线程后立即打印尺寸。他们根本没有在短时间内完成。尝试在打印之前添加
Thread.sleep(1000);,您会看到。 -
谢谢两位,你是对的。在我打印尺寸之前没有完成。
标签: java multithreading concurrency hashmap concurrenthashmap