繁体   English   中英

HashMap,获取时可能是原子的,并且保证密钥已经完全放置

[英]HashMap, maybe atomic when get, and the key is guaranteed already put completely

我的代码:

class Pair {
    public String key;
    public String value;
}
Iterator<Pair> pairs;
HashMap<String, String> map = new HashMap<String, String>();
while (pairs.hasNext()) {
    Pair p = pairs.next();
    map.put(p.ket, p.value)
    // then put p.key to another thread for RPC
    // after returned, lookup the hashmap, join the result and p.value, and output 
}

在每个RPC完成之后,线程池将查找哈希图,以将RPC结果和键值结合在一起。 这是原子的吗?

不, HashMap类不是线程安全的。 您将需要添加其他同步。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM