简体   繁体   English

ConcurrentHashMap put() 方法可以不是线程安全的吗?

[英]Can ConcurrentHashMap put() method be not thread-safe?

Suppose that i have a shared ConcurrentHashMap<String, Integer> called map that has already only one mapping ("One", 1), and suppose also that i have 2 threads.假设我有一个名为 map 的共享 ConcurrentHashMap<String, Integer> 已经只有一个映射(“One”,1),并且还假设我有 2 个线程。

The first thread executes this code:第一个线程执行以下代码:

map.put("One", 2);

and the second thread executes this code:第二个线程执行以下代码:

synchronized (map) {
    Integer number = map.get("One");
    System.out.println(number == map.get("One"));
}

Since ConcurrentHashMap works with lock striping method instead of locking entire object i don't think that the described scenario is thread safe.由于 ConcurrentHashMap 使用锁定条带方法而不是锁定整个 object 我不认为所描述的场景是线程安全的。 Particularly i don't know if there could be an interleaving of map.put("One", 2);特别是我不知道是否可能存在map.put("One", 2);的交错。 in first thread between Integer number = map.get("One");在 Integer 之间的第一个线程Integer number = map.get("One"); call and System.out.println(number == map.get("One"));调用和System.out.println(number == map.get("One")); call in second thread despite both are inside a synchronized block.尽管两个线程都在同步块内,但仍调用第二个线程。

So is it possible that that code prints false?那么该代码是否有可能打印错误?

All methods within ConcurrentHashMap might be thread-safe, but this does not mean that it synchronizes on the ConcurrentHashMap object itself. ConcurrentHashMap中的所有方法都可能是线程安全的,但这并不意味着它在ConcurrentHashMap object 本身上同步。 What you can do is synchronize put and the map access code on the same reference.您可以做的是在同一参考上同步put和 map 访问代码。 Your put code would have to be changed to this:您的put代码必须更改为:

synchronized (map) {
    map.put("One", 2);
}

And your access code can remain like:您的访问代码可以保持如下:

synchronized (map) {
    Integer number = map.get("One");
    System.out.println(number == map.get("One"));
}

This will never be able to print false .这将永远无法打印false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ConcurrentHashMap上进行此操作是否线程安全? - Is it Thread-safe on this operating on ConcurrentHashMap? 如何测试ConcurrentHashMap是否真的是线程安全的? - How can I test that ConcurrentHashMap is truly thread-safe? 如何执行线程安全获取然后使用ConcurrentHashMap删除? - How can I perform a thread-safe get then remove with ConcurrentHashMap? ConcurrentSkipListMap将方法置于线程安全吗? - Is ConcurrentSkipListMap put method thread-safe? ConcurrentHashMap.put() 总是线程安全的吗? 如果是这样,那么为什么它不能正常工作? - is ConcurrentHashMap.put() always Thread-Safe? if so, then why it is not working correctly? ConcurrentHashMap compute() 方法线程安全吗? - Is ConcurrentHashMap compute() method thread safe? 这个字典是否是线程安全的(ConcurrentHashMap + AtomicInteger)? - Is this dictionary function thread-safe (ConcurrentHashMap+AtomicInteger)? ThreadLocal HashMap vs ConcurrentHashMap用于线程安全的未绑定缓存 - ThreadLocal HashMap vs ConcurrentHashMap for thread-safe unbound caches 在ConcurrentHashMap中放置线程安全的同时增加当前值? - Is incrementing of current value while putting thread-safe in ConcurrentHashMap? 我们可以以线程安全的方式从方法中返回 Function 吗? - Can we return Function from a method in a thread-safe way?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM