/// <summary> /// 继续测试4 OK /// BlockingCollection 代替 ConcurrentBag /// </summary> public void 测试4() { var total = 0; Random random = new Random(); using (var q = new BlockingCollection <int>()) { list.AsParallel().ForAll(n => { var c = random.Next(1, 50); Interlocked.Add(ref total, c); for (int i = 0; i < c; i++) { q.Add(Interlocked.Increment(ref num)); //方式2 这样做其实避免了很多不必要的麻烦 //lock (objLock) //{ // num++; // q.Add(num); //} } }); q.CompleteAdding(); Console.WriteLine($"使用内部锁,并发+内部循环{list.Count}次后为:" + num.ToString()); Console.WriteLine($"实际值为:{total + 1}"); Console.WriteLine($"BlockingCollection添加num,集合总数:{q.Count + 1}个"); var l = q.GroupBy(n => n).Where(o => o.Count() > 1); Console.WriteLine($"并发里面使用安全集合BlockingCollection添加num,集合重复值:{l.Count()}个"); Console.ReadKey(); } }
public void Start() { logger.Trace("Starting background processor"); cts = new CancellationTokenSource(); var token = cts.Token; processing_task = Task.Run(() => { while (!collection.IsCompleted && !token.IsCancellationRequested) { try { if (collection.TryTake(out IItem item, 250, token)) { item.Execute(token); } } catch (OperationCanceledException e) { logger.Trace($"Got an OperationCanceledException [{e.Message}]"); } Status = collection.GroupBy(i => i.GetType()) .Select(g => new TypeCountPair(g.Key, g.Count())) .ToList(); } }, token); }
public async Task <Int64> ConsumeMultiAsync(IReceivableSourceBlock <Model[]> source) { // Initialize a counter to track the number of bytes that are processed. Int64 recordsProcessed = 0; // Read from the source buffer until the source buffer has no // available output data. while (await source.OutputAvailableAsync()) { Model[] data; while (source.TryReceive(out data)) { foreach (var item in data) { dataCollection.Add(item); } var duplicates = dataCollection.GroupBy(d => d.GetType() .GetProperties() .Where(p => p.Name == key) .First() .GetValue(d)) .SelectMany(g => g.Skip(1)); var items = duplicates.Except(ValidationResult); foreach (var item in items) { ValidationResult.Add(item); } recordsProcessed += data.Length; } } return(recordsProcessed); }
protected override List <T> CombineChunkedIndicators(BlockingCollection <T> chunkedIndicators, CancellationToken cancellationToken) { var reports = new List <T>(); foreach (var chunkedIndicatorsByCards in chunkedIndicators.GroupBy(GroupBy)) { if (cancellationToken.IsCancellationRequested) { break; } var report = chunkedIndicatorsByCards.First(); chunkedIndicatorsByCards.Skip(1).ForEach(r => { if (cancellationToken.IsCancellationRequested) { return; } report.AddIndicator(r); }); reports.Add(report); } return(reports); }
protected override List <TournamentReportRecord> CombineChunkedIndicators(BlockingCollection <TournamentReportRecord> chunkedIndicators, CancellationToken cancellationToken) { var reports = new List <TournamentReportRecord>(); var groupedChunkedIndicators = chunkedIndicators.GroupBy(x => new { x.BuyIn, x.TableType, x.TournamentSpeed, x.GameType }); foreach (var chunkedIndicatorsGroup in groupedChunkedIndicators) { if (cancellationToken.IsCancellationRequested) { return(reports); } var report = chunkedIndicatorsGroup.First(); chunkedIndicatorsGroup.Skip(1).ForEach(r => report.AddIndicator(r)); reports.Add(report); } return(reports); }
protected override List <TournamentReportRecord> CombineChunkedIndicators(BlockingCollection <TournamentReportRecord> chunkedIndicators, CancellationToken cancellationToken) { var reports = new List <TournamentReportRecord>(); foreach (var chunkedIndicatorsGroup in chunkedIndicators.GroupBy(x => new { x.TournamentId, x.PokerSiteId })) { if (cancellationToken.IsCancellationRequested) { return(reports); } var report = chunkedIndicatorsGroup.First(); chunkedIndicatorsGroup.Skip(1).ForEach(r => report.AddIndicator(r)); reports.Add(report); } return(reports); }
protected override List <ShowdownHandsReportRecord> CombineChunkedIndicators(BlockingCollection <ShowdownHandsReportRecord> chunkedIndicators, CancellationToken cancellationToken) { var reports = new List <ShowdownHandsReportRecord>(); foreach (var chunkedIndicatorsByCards in chunkedIndicators.GroupBy(x => x.ShowdownHand)) { if (cancellationToken.IsCancellationRequested) { return(reports); } var report = chunkedIndicatorsByCards.First(); chunkedIndicatorsByCards.Skip(1).ForEach(r => report.AddIndicator(r)); reports.Add(report); } return(reports); }
public void SafeForConcurrentOperations() { var maxTimeAllowedForTest = TimeSpan.FromSeconds(20); var sw = Stopwatch.StartNew(); try { using (var cts = new CancellationTokenSource(maxTimeAllowedForTest)) { var cancellationToken = cts.Token; var sampleCertificate = SampleCertificate.SampleCertificates[SampleCertificate.CngPrivateKeyId]; var numThreads = 20; var numIterationsPerThread = 1; var exceptions = new BlockingCollection <Exception>(); void Log(string message) => Console.WriteLine($"{sw.Elapsed} {Thread.CurrentThread.Name}: {message}"); WindowsX509CertificateStore.ImportCertificateToStore( Convert.FromBase64String(sampleCertificate.Base64Bytes()), sampleCertificate.Password, StoreLocation.LocalMachine, "My", sampleCertificate.HasPrivateKey); CountdownEvent allThreadsReady = null; CountdownEvent allThreadsFinished = null; ManualResetEventSlim goForIt = new ManualResetEventSlim(false); Thread[] CreateThreads(int number, string name, Action action) => Enumerable.Range(0, number) .Select(i => new Thread(() => { allThreadsReady.Signal(); goForIt.Wait(cancellationToken); for (int j = 0; j < numIterationsPerThread; j++) { try { Log($"{name} {j}"); action(); } catch (Exception e) { Log(e.ToString()); exceptions.Add(e); } } allThreadsFinished.Signal(); }) { Name = $"{name}#{i}" }).ToArray(); var threads = CreateThreads(numThreads, "ImportCertificateToStore", () => { WindowsX509CertificateStore.ImportCertificateToStore( Convert.FromBase64String(sampleCertificate.Base64Bytes()), sampleCertificate.Password, StoreLocation.LocalMachine, "My", sampleCertificate.HasPrivateKey); }) .Concat(CreateThreads(numThreads, "AddPrivateKeyAccessRules", () => { WindowsX509CertificateStore.AddPrivateKeyAccessRules( sampleCertificate.Thumbprint, StoreLocation.LocalMachine, "My", new List <PrivateKeyAccessRule> { new PrivateKeyAccessRule("BUILTIN\\Users", PrivateKeyAccess.FullControl) }); })) .Concat(CreateThreads(numThreads, "GetPrivateKeySecurity", () => { var unused = WindowsX509CertificateStore.GetPrivateKeySecurity( sampleCertificate.Thumbprint, StoreLocation.LocalMachine, "My"); })).ToArray(); allThreadsReady = new CountdownEvent(threads.Length); allThreadsFinished = new CountdownEvent(threads.Length); foreach (var thread in threads) { thread.Start(); } allThreadsReady.Wait(cancellationToken); goForIt.Set(); allThreadsFinished.Wait(cancellationToken); foreach (var thread in threads) { Log($"Waiting for {thread.Name} to join..."); if (!thread.Join(TimeSpan.FromSeconds(1))) { Log($"Aborting {thread.Name}"); thread.Abort(); } } sw.Stop(); sampleCertificate.EnsureCertificateNotInStore("My", StoreLocation.LocalMachine); if (exceptions.Any()) { throw new AssertionException( $"The following exceptions were thrown during the test causing it to fail:{Environment.NewLine}{string.Join($"{Environment.NewLine}{new string('=', 80)}", exceptions.GroupBy(ex => ex.Message).Select(g => g.First().ToString()))}"); } if (sw.Elapsed > maxTimeAllowedForTest) { throw new TimeoutException( $"This test exceeded the {maxTimeAllowedForTest} allowed for this test to complete."); } } } catch (OperationCanceledException) { throw new TimeoutException($"This test took longer than {maxTimeAllowedForTest} to run"); } }