Nel codice seguente:ContainsKey Discussione sicuro
public class StringCache
{
private readonly object lockobj = new object();
private readonly Dictionary<int, string> cache = new Dictionary<int, string>();
public string GetMemberInfo(int key)
{
if (cache.ContainsKey(key))
return cache[key];
lock (lockobj)
{
if (!cache.ContainsKey(key))
cache[key] = GetString(key);
}
return cache[key];
}
private static string GetString(int key)
{
return "Not Important";
}
}
1) è thread ContainsKey sicuro? IOW, cosa succede se quel metodo è in esecuzione quando un altro thread sta aggiungendo qualcosa al dizionario? 2) Per la prima cache di ritorno [chiave], c'è qualche possibilità che possa restituire un valore alterato?
TIA,
MB
Collegamento non trovato (404). – avenmore