ConcurrentDictionary与多键 [英] ConcurrentDictionary with multi key
本文介绍了ConcurrentDictionary与多键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一种somoe类型的多键字典我可以安全地用于多个任务吗?
我的意思是我需要一个带有2个线程安全的字典,我可以在多任务程序中使用它。
类似的东西:
There is a somoe sort of multi-key dictionary that I can safely use with multiple Tasks?
I mean that I need a Dictionary with 2 keys threadsafe that I can use in multi task program.
Something like that:
var dictionary = new MultiDictionary<int key1, int key2, string value>();
Task[] tasks = new Task[2];
tasks[0] = Task.Run(() =>
{
for (int i = 0; i < 2; i++)
{
dictionary.Add(i,i+1, "value: "+i);
}
});
tasks[1] = Task.Run(() =>
{
for (int i = 2; i < data.Length; i++)
{
if (dictionary.ContainsKeys(i,i+1)
Console.WriteLine("Value is:"dictionary.GetValue(i,i+1));
else
Console.WriteLine(No value);
}
});
Task.WaitAll(tasks);
感谢'的
Thank''s
推荐答案
如果你必须提供两个键来获取你的价值然后你使用1键,这样你就可以连接(以某种方式结合2个密钥,如果你可以创建一个长的例子等)两个键并使用普通的字典
。
If you have to supply both keys to get your value then you are using 1 key so you can "concat" (somehow combine 2 key in the case of ints you can create a long for example etc.) the two keys and use a normal Dictionary
.
因为您的问题实际上是关于 MultiDictionary
的实现,如下所述:C#多键通用词典 [ ^ ]你应该在那篇文章的问题部分询问。
Since your question is really about the implementation of the MultiDictionary
as described in: C# Multi-key Generic Dictionary[^] you should ask in the questions section in that article.
这篇关于ConcurrentDictionary与多键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文