假设我有:
MyCollection = new ConcurrentDictionary<string, int>();
我现在可以安全地从MyCollection
. 但是修改项目呢。例如,这样做是否安全:
MyCollection["key"] = 1; // thread 1
和
MyCollection["key"] = 2; // thread 2
示例 2:
MyCollection2 = new ConcurrentDictionary<string, List<int>>();
这样做安全吗?
MyCollection2["key"].Add(1); // Thread1
和
MyCollection2["key"].Add(2); // Thread2
其中 Thread1 和 Thread2 同时执行。修改项目时是否必须创建锁?