我找不到关于ConcurrentDictionary类型的足够信息,所以我想我会在这里问一下.
目前,我使用a Dictionary来保存由多个线程(来自线程池,因此没有确切数量的线程)不断访问的所有用户,并且它具有同步访问权限.
我最近发现在.NET 4.0中有一组线程安全的集合,它似乎非常令人愉快.我想知道,什么是"更有效和更容易管理"的选项,因为我可以选择正常Dictionary的同步访问,或者具有ConcurrentDictionary已经线程安全的选项.
这更像是一个学术问题......但是ConcurrentDictionary.TryAdd会失败吗?如果是这样,在什么情况下,为什么?
我总是对选择哪一个感到困惑.当我看到它,我用Dictionary了List,如果我想两个数据类型为Key和Value这样我就可以很容易地找到它的价值key,但我总是困惑,如果我要使用一个ConcurrentDictionary或Dictionary?
在我离开之前没有对此进行太多研究之前我已经尝试过,但似乎谷歌并没有真正得到任何关于Dictionaryvs ConcurrentDictionary但是每个人都有一些东西.
我之前曾问过这样的朋友,但他们所说的只是:" ConcurrentDictionary如果你在代码中使用了很多字典就会使用",我真的不想纠缠他们来更详细地解释它.任何人都可以扩展这个吗?
我有一个使用ConcurrentDictionary实现的缓存,我需要保留的数据取决于5个参数.所以从缓存中获取它的方法是:(为简单起见,这里只显示3个参数,我更改了数据类型以表示CarData的清晰度)
public CarData GetCarData(string carModel, string engineType, int year);
Run Code Online (Sandbox Code Playgroud)
我想知道在我的ConcurrentDictionary中使用哪种类型的密钥会更好,我可以这样做:
var carCache = new ConcurrentDictionary<string, CarData>();
// check for car key
bool exists = carCache.ContainsKey(string.Format("{0}_{1}_{2}", carModel, engineType, year);
Run Code Online (Sandbox Code Playgroud)
或者像这样:
var carCache = new ConcurrentDictionary<Tuple<string, string, int>, CarData>();
// check for car key
bool exists = carCache.ContainsKey(new Tuple(carModel, engineType, year));
Run Code Online (Sandbox Code Playgroud)
我不会将这些参数与其他任何地方一起使用,因此没有理由创建一个类来保持它们在一起.
我想知道哪种方法在性能和可维护性方面更好.
我使用并发字典作为线程安全的静态缓存,并注意到以下行为:
如果在不同的线程上同时调用GetOrAdd,可能会多次调用addValueFactory,但是对于每个调用,它的键/值对可能不会添加到字典中.
我希望能够保证工厂只被召唤一次.有没有办法使用ConcurrentDictionary API执行此操作而不依赖于我自己的单独同步(例如锁定valueFactory)?
我的用例是valueFactory在动态模块中生成类型,所以如果同一个键的两个valueFactories同时运行,我点击:
System.ArgumentException: Duplicate type name within an assembly.
我有一个ConcurrentDictionary对象,我想将其设置为Dictionary对象.
不允许在他们之间施放.那我该怎么做?
我正在寻找一种缓存异步操作结果的优雅方法.
我第一次有这样的同步方法:
public String GetStuff(String url)
{
WebRequest request = WebRequest.Create(url);
using (var response = request.GetResponse())
using (var sr = new StreamReader(response.GetResponseStream()))
return sr.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)
然后我让它异步:
public async Task<String> GetStuffAsync(String url)
{
WebRequest request = WebRequest.Create(url);
using (var response = await request.GetResponseAsync())
using (var sr = new StreamReader(response.GetResponseStream()))
return await sr.ReadToEndAsync();
}
Run Code Online (Sandbox Code Playgroud)
然后我决定我应该缓存结果,所以我不需要经常在外面查询:
ConcurrentDictionary<String, String> _cache = new ConcurrentDictionary<String, String>();
public async Task<String> GetStuffAsync(String url)
{
return _cache.GetOrAdd(url, await GetStuffInternalAsync(url));
}
private async Task<String> GetStuffInternalAsync(String url)
{
WebRequest request = …Run Code Online (Sandbox Code Playgroud) 相关简介信息:
AFAIK,并发堆栈,队列和包类在内部使用链接列表实现.
而且我知道争用少得多,因为每个线程都负责自己的链表.无论如何,我的问题是关于ConcurrentDictionary<,>
但我正在测试这段代码:(单线程)
Stopwatch sw = new Stopwatch();
sw.Start();
var d = new ConcurrentDictionary < int, int > ();
for(int i = 0; i < 1000000; i++) d[i] = 123;
for(int i = 1000000; i < 2000000; i++) d[i] = 123;
for(int i = 2000000; i < 3000000; i++) d[i] = 123;
Console.WriteLine("baseline = " + sw.Elapsed);
sw.Restart();
var d2 = new Dictionary < int, int > ();
for(int i = 0; i < 1000000; i++) lock (d2) …Run Code Online (Sandbox Code Playgroud) 我正在使用并发字典来存储大约200万条记录,并希望知道将字典的并发级别初始化为什么.
MSDN页面的示例代码中包含以下注释:
concurrencyLevel越高,可以在ConcurrentDictionary上同时执行的理论操作数越高.但是,随着concurrencyLevel的上升,调整字典大小等全局操作会花费更长的时间.
这是我能找到的最好的解释concurrencyLevel的含义,但它仍然很模糊.
我有一个静态类,我在其中使用字典作为查找表来映射.NET类型和SQL类型.这是一个这样的字典的例子:
private static readonly Dictionary<Type, string> SqlServerMap = new Dictionary<Type, string>
{
{typeof (Boolean), "bit"},
{typeof (Byte[]), "varbinary(max)"},
{typeof (Double), "float"},
{typeof (Byte), "tinyint"},
{typeof (Int16), "smallint"},
{typeof (Int32), "int"},
{typeof (Int64), "bigint"},
{typeof (Decimal), "decimal"},
{typeof (Single), "real"},
{typeof (DateTime), "datetime2(7)"},
{typeof (TimeSpan), "time"},
{typeof (String), "nvarchar(MAX)"},
{typeof (Guid), "uniqueidentifier"}
};
Run Code Online (Sandbox Code Playgroud)
然后我在下面有一个公共方法,它传入一个.NET类型,它使用这个字典返回相应的MS SQL Server类型的字符串值.但是,由于这被用作进行数据库查询的查找表,我认为将其作为一个有意义ConcurrentDictionary.我改成了:
private static readonly IDictionary<Type, string> SqlServerMap = new ConcurrentDictionary<Type, string>
{
{typeof (Boolean), "bit"},
{typeof (Byte[]), "varbinary(max)"},
{typeof (Double), "float"},
{typeof (Byte), "tinyint"}, …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×4
dictionary ×4
caching ×2
concurrency ×2
.net-4.0 ×1
async-await ×1
equality ×1
idictionary ×1
private ×1