本文整理汇总了C#中ConcurrentBag.Distinct方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentBag.Distinct方法的具体用法?C# ConcurrentBag.Distinct怎么用?C# ConcurrentBag.Distinct使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentBag
的用法示例。
在下文中一共展示了ConcurrentBag.Distinct方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: should_generate_distinct_handles
public void should_generate_distinct_handles()
{
var list = new ConcurrentBag<string>();
Parallel.For(0, 10000, i => list.Add(HandleGenerator.Generate(100)));
Assert.Equal(10000, list.Distinct().Count());
}
示例2: Instance_ThreadSafe
public void Instance_ThreadSafe()
{
using (var gate = new Barrier(5))
{
var result = new ConcurrentBag<AnyConstructorFinder>();
Action test = () =>
{
gate.SignalAndWait(20);
var instance = AnyConstructorFinder.Instance;
Thread.MemoryBarrier();
result.Add(instance);
};
var cycleState = Parallel.For(0, 200,
new ParallelOptions { MaxDegreeOfParallelism = 15 },
x => { test(); })
;
while (!cycleState.IsCompleted)
{
Thread.Sleep(100);
}
Assert.IsTrue(result.All(x => x != null));
Assert.IsTrue(result.Distinct().Count() == 1);
}
}
示例3: ItHasBeenFixed
public async Task ItHasBeenFixed()
{
var activator = new BuiltinHandlerActivator();
Using(activator);
var receivedMessageIds = new ConcurrentBag<string>();
activator.Handle<string>(async (_, context, message) =>
{
receivedMessageIds.Add(context.TransportMessage.Headers[Headers.MessageId]);
});
var bus = Configure.With(activator)
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "buggerino"))
.Start();
var customHeaders = new Dictionary<string, string>
{
{"custom-header", "woohoo"}
};
const string repeatedMessage = "hej med dig";
await bus.SendLocal(repeatedMessage, customHeaders);
await bus.SendLocal("hej igen med", customHeaders);
await bus.SendLocal(repeatedMessage, customHeaders);
await Task.Delay(TimeSpan.FromSeconds(1));
Assert.That(receivedMessageIds.Distinct().Count(), Is.EqualTo(3), "Expected three unique message IDs - got: {0}", string.Join(", ", receivedMessageIds));
}
示例4: Should_use_all_threads_for_many_tasks
public void Should_use_all_threads_for_many_tasks()
{
var threadIds = new ConcurrentBag<int>();
var atomicCounter = new AtomicCounter(0);
Action callback = () =>
{
atomicCounter.GetAndIncrement();
threadIds.Add(Thread.CurrentThread.ManagedThreadId);
};
for (var i = 0; i < 1000; i++)
{
Factory.StartNew(callback);
}
//spin until work is completed
SpinWait.SpinUntil(() => atomicCounter.Current == 1000, TimeSpan.FromSeconds(1));
Assert.AreEqual(Pool.Settings.NumThreads, threadIds.Distinct().Count());
}
示例5: Execute
public int Execute()
{
var fileItems = File.ReadAllLines(_file).Select(Int64.Parse).ToArray();
var hashSet = new HashSet<Int64>(fileItems);
var result = new ConcurrentBag<Int64>();
Parallel.ForEach(fileItems, x =>
{
var y1 = IntervalStart - x;
var y2 = IntervalEnd - x;
for (var y = y1; y <= y2; y++)
{
if (hashSet.Contains(y))
result.Add(x + y);
}
});
return result.Distinct().Count();
}
示例6: WhenTwoThreadsInitialiseASharedInstanceSimultaneouslyViaChildLifetime_OnlyOneInstanceIsActivated
public void WhenTwoThreadsInitialiseASharedInstanceSimultaneouslyViaChildLifetime_OnlyOneInstanceIsActivated()
{
int activationCount = 0;
var results = new ConcurrentBag<object>();
var exceptions = new ConcurrentBag<Exception>();
var builder = new ContainerBuilder();
builder.Register(c =>
{
Interlocked.Increment(ref activationCount);
Thread.Sleep(500);
return new object();
})
.SingleInstance();
var container = builder.Build();
ThreadStart work = () => {
try
{
var o = container.BeginLifetimeScope().Resolve<object>();
results.Add(o);
}
catch (Exception ex)
{
exceptions.Add(ex);
}
};
var t1 = new Thread(work);
var t2 = new Thread(work);
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Assert.Equal(1, activationCount);
Assert.Empty(exceptions);
Assert.Equal(1, results.Distinct().Count());
}
示例7: ObjectPool_should_not_leak_when_used_properly
public Property ObjectPool_should_not_leak_when_used_properly(Tuple<int, int>[] values)
{
var tasks = new List<Task<bool>>();
var pooledObjects = new ConcurrentBag<MyPooledObject>();
Func<Tuple<int, int>, MyPooledObject> setPool = tupe =>
{
var obj = _pool.Take();
obj.Num = tupe.Item1;
obj.Num2 = tupe.Item2;
return obj;
};
Func<MyPooledObject, Tuple<int, int>, bool> freePoolAndAssertReferentialIntegrity = (o, tuple) =>
{
var propsEqual = o.Num == tuple.Item1 && o.Num2 == tuple.Item2;
pooledObjects.Add(o); //add a reference to O
o.Recycle();
return propsEqual;
};
foreach (var value in values)
{
var v = value;
var task =
Task.Run(() => setPool(v)).ContinueWith(t => freePoolAndAssertReferentialIntegrity(t.Result, v));
tasks.Add(task);
}
var results = Task.WhenAll(tasks);
if (!results.Wait(200))
return false.Label($"Should not have taken 200ms to process {values.Length} items");
if (!results.Result.All(x => x))
return false.Label("None of the objects in the pool should ever be concurrently modified while in use");
var count = pooledObjects.Distinct().Count();
return
(count <= ObjectCount).Label(
$"Should not have produced more than {ObjectCount}, but was instead {count}");
}
示例8: Should_process_workload_across_exactly_DedicatedThreadPoolSettings_NumThreads
public void Should_process_workload_across_exactly_DedicatedThreadPoolSettings_NumThreads()
{
var numThreads = Environment.ProcessorCount;
var threadIds = new ConcurrentBag<int>();
var atomicCounter = new AtomicCounter(0);
Action callback = () =>
{
atomicCounter.GetAndIncrement();
threadIds.Add(Thread.CurrentThread.ManagedThreadId);
};
using (var threadPool = new DedicatedThreadPool(new DedicatedThreadPoolSettings(numThreads)))
{
for (var i = 0; i < 1000; i++)
{
threadPool.QueueUserWorkItem(callback);
}
//spin until work is completed
SpinWait.SpinUntil(() => atomicCounter.Current == 1000, TimeSpan.FromSeconds(1));
}
Assert.AreEqual(numThreads, threadIds.Distinct().Count());
}
示例9: TestSaveUpdatesAutoIncrementingField
public void TestSaveUpdatesAutoIncrementingField()
{
//---------------Set up test pack-------------------
ClassDef.ClassDefs.Clear();
TestAutoInc.LoadClassDefWithAutoIncrementingID();
var newIds = new ConcurrentBag<int?>();
//---------------Execute Test ----------------------
Parallel.For(0, 1000, i => {
//---------------Set up test pack-------------------
var bo = new TestAutoInc();
bo.SetPropertyValue("testfield", "testing 123");
//---------------Assert Precondition----------------
Assert.IsFalse(bo.TestAutoIncID.HasValue);
//---------------Execute Test ----------------------
bo.Save();
//---------------Test Result -----------------------
newIds.Add(bo.TestAutoIncID);
});
//---------------Test Result -----------------------
Assert.IsTrue(newIds.All(i => i.HasValue));
Assert.IsTrue(newIds.All(i => i > 0));
Assert.That(newIds.Distinct().Count(), Is.EqualTo(1000), "Every generated ID must be unique");
}
示例10: World_should_not_end_if_exception_thrown_in_user_callback
public void World_should_not_end_if_exception_thrown_in_user_callback()
{
var numThreads = 3;
var threadIds = new ConcurrentBag<int>();
Action badCallback = () =>
{
threadIds.Add(Thread.CurrentThread.ManagedThreadId);
throw new Exception("DEATH TO THIS THREAD I SAY!");
};
Action goodCallback = () =>
{
threadIds.Add(Thread.CurrentThread.ManagedThreadId);
};
using (var threadPool = new DedicatedThreadPool(new DedicatedThreadPoolSettings(numThreads, TimeSpan.FromSeconds(1))))
{
for (var i = 0; i < numThreads; i++)
{
threadPool.QueueUserWorkItem(badCallback);
Thread.Sleep(20);
}
//sanity check
Assert.AreEqual(numThreads, threadIds.Distinct().Count());
//run the job again. Should get 3 more managed thread IDs
for (var i = 0; i < numThreads*10; i++)
{
threadPool.QueueUserWorkItem(goodCallback);
Thread.Sleep(20);
}
}
// half of thread IDs should belong to failed threads, other half to successful ones
Assert.AreEqual(numThreads * 2, threadIds.Distinct().Count());
}
示例11: button1_Click
private void button1_Click(object sender, EventArgs e)
{
var cnt = tbCount.Text.AsInt();
var bag = new ConcurrentBag<FID>();
if (chkParallel.Checked)
Parallel.For(0,cnt,(i)=>
{
bag.Add( FID.Generate() );
});
else
for(var i=0; i<cnt;i++)
bag.Add( FID.Generate() );
var sb = new StringBuilder();
var c=0;
foreach(var id in bag)
{
sb.AppendLine( "{0}: {1} -> {2}".Args(c, id.ID, id) );
c++;
if (c>10000)
{
sb.AppendLine("......more......");
break;
}
}
//Uncomment to cause duplicates
//var v =bag.FirstOrDefault();
//bag.Add(v);
//bag.Add(v);//duplicate
if (bag.Count==bag.Distinct().Count())
sb.Insert(0, "No Duplicates in the set of {0:n2}\r\n".Args(bag.Count));
else
sb.Insert(0, "DUPLICATES!!!!!!!!!!!!! in the set of {0:n2}\r\n\r\n\r\n".Args(bag.Count));
tbDump.Text = sb.ToString();
}
示例12: UseSameThreadForAllRequests
public void UseSameThreadForAllRequests()
{
var bag = new ConcurrentBag<int>();
using (var server = new HttpServer("http://*:1234/"))
{
server.RAW("")
.Subscribe(ctx =>
{
bag.Add(Thread.CurrentThread.ManagedThreadId);
ctx.Respond(200);
});
Parallel.For(1, 1000, i => Browser.ExecuteGet("http://localhost:1234/"));
bag.Distinct().Count()
.Should("The default scheduler should be Event Loop, and all subscriber run in same thread")
.Be.EqualTo(1);
}
}
示例13: ProcessPics
private static void ProcessPics(Task t, ConcurrentBag<Uri> picsToGet)
{
if (t.Exception != null)
{
throw t.Exception.Flatten();
}
string[] existingFiles = Directory.GetFiles(Settings.Default.FacebookUsersSettings.PhotoDirectory);
IEnumerable<string> filesToDelete =
existingFiles.Except(
picsToGet.Distinct().Select(
x =>
Path.Combine(Settings.Default.FacebookUsersSettings.PhotoDirectory,
Path.GetFileName(x.AbsoluteUri))));
try
{
foreach (string file in filesToDelete)
{
File.Delete(file);
}
}
catch
{
}
//delete files not in list
IEnumerable<Uri> filesToDownload =
picsToGet.Distinct().Where(
x =>
!existingFiles.Contains(Path.Combine(Settings.Default.FacebookUsersSettings.PhotoDirectory,
Path.GetFileName(x.AbsoluteUri))));
Parallel.ForEach(filesToDownload, DownloadFiles);
}
示例14: GetPeopleTitles
async static Task<string[]> GetPeopleTitles(string listTitle, List<string> previousLists = null, int level = 0)
{
if ((previousLists != null && previousLists.Contains(listTitle)) || level >= 2)
return new string[0];
Console.WriteLine("Getting {0}.", listTitle);
try
{
string peoplePage = await Utilities.GetPage(listTitle);
if (previousLists == null)
previousLists = new List<string>();
previousLists.Add(listTitle);
var peopleTitles = new List<string>();
Regex personRegex = new Regex(@"(?<=\*[^\[]*\[\[)[^\[\]\|]+");
var matches = personRegex.Matches(peoplePage);
foreach (Match match in matches)
{
if (!match.Value.Contains("Category") && !match.Value.Contains("List") && !match.Value.Contains(" people") && !match.Value.Contains(':'))
{
peopleTitles.Add(match.Value.Replace(' ', '_'));
}
}
// check for other people lists in this list
Regex listRegex = new Regex(@"(?<=\[\[)List of[^\]]+");
var listMatches = listRegex.Matches(peoplePage);
if (listMatches.Count > 0)
{
var listNames = new List<string>();
foreach (Match match in listMatches)
{
listNames.Add(match.Value);
}
var titles = new ConcurrentBag<string>();
Parallel.ForEach(listNames, async name =>
{
if (!previousLists.Contains(name))
{
var nestedTitles = await GetPeopleTitles(name, previousLists, level + 1);
foreach (var title in nestedTitles)
{
titles.Add(title);
}
}
});
peopleTitles.AddRange(titles.Distinct());
}
return peopleTitles.ToArray();
}
catch
{
return new string[0];
}
}
示例15: AttentionSpiderParallelExecute
/// <summary>
/// 并行获取关注的UID列表
/// </summary>
/// <param name="uid"></param>
/// <returns></returns>
public static List<long> AttentionSpiderParallelExecute(string uid)
{
ConcurrentBag<long> concurrentBag = new ConcurrentBag<long>();
var weiboUser = AnalyseCnPage.AnalysisUserHome(GetWeiboUser(uid));
var totalPage = weiboUser.FriendsCount / 10 + 1;
Action<int> action = page =>
{
//对于网络错误进行重试
for (int i = 0; i < 3; i++)
{
var pageStr = GetFollowers(weiboUser, page);
var pageList = AnalyseCnPage.AnalysisFollowers(pageStr);
if (pageList != null)
{
foreach (long id in pageList)
{
concurrentBag.Add(id);
}
break;
}
CNHttpWorkLogger.Info("用户{0}第{1}页关注获取失败", uid, page);
}
};
ParallelOptions po = new ParallelOptions { MaxDegreeOfParallelism = 32 };
Parallel.For(1, totalPage, po, action);
return concurrentBag.Distinct().ToList();
}