请考虑这段代码:
public static class SomeClass
{
private static List<Item> Items;
//Constructor
public static void AddToChache(string key, object item)
{
Items.Add(new Item(){ key = key , content = item, DateAdded = DateTime.Now});
}
public static List<Item> GetAllItems()
{
return Items;
}
....
如果我想在应用层创建一个缓存,这个方法是否需要lock
来处理并发?
我想要线程安全的方法。
最佳答案
If I want to create a cache in application level, does this methods need lock to handle concurrency?
不,如果你改用线程安全的集合,例如ConcurrentDictionary
,否则是 - 它确实如此。
关于c# - 锁定静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47941742/