本文整理汇总了C#中ConcurrentDictionary.ElementAt方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentDictionary.ElementAt方法的具体用法?C# ConcurrentDictionary.ElementAt怎么用?C# ConcurrentDictionary.ElementAt使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentDictionary
的用法示例。
在下文中一共展示了ConcurrentDictionary.ElementAt方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Parallel_PutGetDelete_Random
public void Parallel_PutGetDelete_Random()
{
const int PUTTER_CNT = 2, PUTTER_OP_CNT = 2 * 10000;
const int GETTER_CNT = 6, GETTER_OP_CNT = 2 * 30000;
const int DELETER_CNT = 2, DELETER_OP_CNT = 2 * 10000;
var data = new ConcurrentDictionary<PilePointer, string>();
var getAccessViolations = new ConcurrentDictionary<int, int>();
var deleteAccessViolations = new ConcurrentDictionary<int, int>();
using (var pile = new DefaultPile())
{
pile.Start();
var ipile = pile as IPile;
// putter tasks
var putters = new Task[PUTTER_CNT];
for (int it = 0; it < PUTTER_CNT; it++)
{
var task = new Task(() => {
for (int i = 0; i < PUTTER_OP_CNT; i++)
{
var str = NFX.Parsing.NaturalTextGenerator.Generate();
var pp = ipile.Put(str);
data.TryAdd(pp, str);
}
});
putters[it] = task;
}
// getter tasks
var getters = new Task[GETTER_CNT];
for (int it = 0; it < GETTER_CNT; it++)
{
var task = new Task(() => {
for (int i = 0; i < GETTER_OP_CNT; i++)
{
if (data.Count == 0) {
System.Threading.Thread.Yield();
continue;
}
var idx = ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, data.Count-1);
var kvp = data.ElementAt(idx);
try
{
var str = ipile.Get(kvp.Key);
Assert.AreEqual(str, kvp.Value);
}
catch (PileAccessViolationException)
{
getAccessViolations.AddOrUpdate(System.Threading.Thread.CurrentThread.ManagedThreadId, 1, (mid, val) => val + 1);
}
}
});
getters[it] = task;
}
// deleter tasks
var deleters = new Task[DELETER_CNT];
for (int it = 0; it < DELETER_CNT; it++)
{
var task = new Task(() => {
for (int i = 0; i < DELETER_OP_CNT; i++)
{
if (data.Count == 0) {
System.Threading.Thread.Yield();
continue;
}
var idx = ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, data.Count-1);
var kvp = data.ElementAt(idx);
try
{
ipile.Delete(kvp.Key);
}
catch (PileAccessViolationException)
{
deleteAccessViolations.AddOrUpdate(System.Threading.Thread.CurrentThread.ManagedThreadId, 1, (mid, val) => val + 1);
}
}
});
deleters[it] = task;
}
foreach (var task in putters) task.Start();
foreach (var task in getters) task.Start();
foreach (var task in deleters) task.Start();
Task.WaitAll(putters.Concat(getters).Concat(deleters).ToArray());
foreach (var kvp in getAccessViolations)
//.........这里部分代码省略.........