标签: concurrentdictionary

.NET - 字典锁定与ConcurrentDictionary

我找不到关于ConcurrentDictionary类型的足够信息,所以我想我会在这里问一下.

目前,我使用a Dictionary来保存由多个线程(来自线程池,因此没有确切数量的线程)不断访问的所有用户,并且它具有同步访问权限.

我最近发现在.NET 4.0中有一组线程安全的集合,它似乎非常令人愉快.我想知道,什么是"更有效和更容易管理"的选项,因为我可以选择正常Dictionary的同步访问,或者具有ConcurrentDictionary已经线程安全的选项.

参考.NET 4.0 ConcurrentDictionary

.net concurrency dictionary concurrentdictionary

121
推荐指数
6
解决办法
6万
查看次数

ConcurrentDictionary.TryAdd可以失败吗?

这更像是一个学术问题......但是ConcurrentDictionary.TryAdd会失败吗?如果是这样,在什么情况下,为什么?

c# concurrentdictionary

65
推荐指数
2
解决办法
2万
查看次数

我什么时候应该使用ConcurrentDictionary和Dictionary?

我总是对选择哪一个感到困惑.当我看到它,我用DictionaryList,如果我想两个数据类型为KeyValue这样我就可以很容易地找到它的价值key,但我总是困惑,如果我要使用一个ConcurrentDictionaryDictionary

在我离开之前没有对此进行太多研究之前我已经尝试过,但似乎谷歌并没有真正得到任何关于Dictionaryvs ConcurrentDictionary但是每个人都有一些东西.

我之前曾问过这样的朋友,但他们所说的只是:" ConcurrentDictionary如果你在代码中使用了很多字典就会使用",我真的不想纠缠他们来更详细地解释它.任何人都可以扩展这个吗?

c# dictionary concurrentdictionary

38
推荐指数
4
解决办法
1万
查看次数

元组vs字符串作为C#中的字典键

我有一个使用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)

我不会将这些参数与其他任何地方一起使用,因此没有理由创建一个类来保持它们在一起.

我想知道哪种方法在性能和可维护性方面更好.

.net c# caching equality concurrentdictionary

27
推荐指数
4
解决办法
7912
查看次数

为什么ConcurrentDictionary.GetOrAdd(key,valueFactory)允许调用valueFactory两次?

我使用并发字典作为线程安全的静态缓存,并注意到以下行为:

来自GetOrAdd上的MSDN文档:

如果在不同的线程上同时调用GetOrAdd,可能会多次调用addValueFactory,但是对于每个调用,它的键/值对可能不会添加到字典中.

我希望能够保证工厂只被召唤一次.有没有办法使用ConcurrentDictionary API执行此操作而不依赖于我自己的单独同步(例如锁定valueFactory)?

我的用例是valueFactory在动态模块中生成类型,所以如果同一个键的两个valueFactories同时运行,我点击:

System.ArgumentException: Duplicate type name within an assembly.

c# concurrency multithreading caching concurrentdictionary

26
推荐指数
2
解决办法
5796
查看次数

如何将ConcurrentDictionary转换为Dictionary?

我有一个ConcurrentDictionary对象,我想将其设置为Dictionary对象.

不允许在他们之间施放.那我该怎么做?

.net c# dictionary concurrentdictionary

23
推荐指数
3
解决办法
2万
查看次数

缓存异步操作

我正在寻找一种缓存异步操作结果的优雅方法.

我第一次有这样的同步方法:

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)

.net c# multithreading async-await concurrentdictionary

18
推荐指数
2
解决办法
7484
查看次数

ConcurrentDictionary <>在一个线程误解的表现?

相关简介信息:

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)

c# .net-4.0 task-parallel-library concurrentdictionary

17
推荐指数
3
解决办法
2万
查看次数

如何选择并发字典的并发级别

我正在使用并发字典来存储大约200万条记录,并希望知道将字典的并发级别初始化为什么.

MSDN页面的示例代码中包含以下注释:

concurrencyLevel越高,可以在ConcurrentDictionary上同时执行的理论操作数越高.但是,随着concurrencyLevel的上升,调整字典大小等全局操作会花费更长的时间.

这是我能找到的最好的解释concurrencyLevel的含义,但它仍然很模糊.

concurrentdictionary

14
推荐指数
1
解决办法
3322
查看次数

如何初始化ConcurrentDictionary?错误:"无法访问私有方法'在此处添加'

我有一个静态类,我在其中使用字典作为查找表来映射.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# dictionary private idictionary concurrentdictionary

13
推荐指数
3
解决办法
5328
查看次数