0

有时,我们需要更新其中field一个entry.value. 线程安全的方法是构造一个新的entry.value并使用put方法来更新。也就是说,即使我只是做了一些小的修改,我也需要对原始值进行深度复制。

我可以像这样进行此更新吗

map[key].field = fieldValue;
4

2 回答 2

1

Hash map returns an entry safely, but assignment of a field is out of scope of the map. So, you should do something here for thread safety. Your code is equivalent to:

Entry entry = map[key];
entry.field = fieldValue;

Obviously the field assignment operator doesn't know anything about the map.

于 2013-05-02T14:32:21.867 回答
0

也许需要对条目本身进行锁定。如果它只是一个作业,那么 volatile 。

于 2013-07-10T01:28:52.283 回答