当前位置: 首页>>代码示例>>C#>>正文


C# ConcurrentSet.AddOrThrow方法代码示例

本文整理汇总了C#中ConcurrentSet.AddOrThrow方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentSet.AddOrThrow方法的具体用法?C# ConcurrentSet.AddOrThrow怎么用?C# ConcurrentSet.AddOrThrow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ConcurrentSet的用法示例。


在下文中一共展示了ConcurrentSet.AddOrThrow方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RunAsync

      public async Task RunAsync() {
         Console.WriteLine(DateTime.Now);
         const int kMessagesPerWorker = 200000;
         var sink = courierFacades[0];
         var senders = courierFacades.Skip(1).ToArray();
         var counter = kMessagesPerWorker * senders.Length;
         var doneSignal = new AsyncLatch();
         int upCounter = 0;
         var set = new ConcurrentSet<int>();
         sink.InboundMessageRouter.RegisterHandler<string>(
            x => {
               set.AddOrThrow(int.Parse(x.Body));
               var newCounter = Interlocked.Decrement(ref counter);
               Interlocked.Increment(ref upCounter);
               if (upCounter % 500 == 0)
                  Console.WriteLine(newCounter + " " + upCounter);
               if (newCounter == 0) {
                  doneSignal.Set();
               }
               return Task.FromResult(false);
            });

         var sync = new AsyncCountdownLatch(senders.Length);
         var senderTasks = senders.Select((s, id) => Go(async () => {
            await s.PeerTable.GetOrAdd(sink.Identity.Id).WaitForDiscoveryAsync().ConfigureAwait(false);
            sync.Signal();
            await sync.WaitAsync().ConfigureAwait(false);
            Console.WriteLine("Sink discovered: " + DateTime.Now);

            const int kBatchFactor = 1;
            for (var batch = 0; batch < kBatchFactor; batch++) {
               var batchSize = kMessagesPerWorker / kBatchFactor;
               await Task.WhenAll(Util.Generate(
                  batchSize,
                  i => s.Messenger.SendReliableAsync(
                     "" + (batch * batchSize + i + id * kMessagesPerWorker),
                     sink.Identity.Id))
                  ).ConfigureAwait(false);
            }

            Console.WriteLine("Worker Done: " + DateTime.Now);
         }));
         await Task.WhenAll(senderTasks).ConfigureAwait(false);
         Console.WriteLine("Senders Done: " + DateTime.Now);

         await doneSignal.WaitAsync().ConfigureAwait(false);
         Console.WriteLine("Done Signalled: " + DateTime.Now);

         AssertCollectionDeepEquals(set, new ConcurrentSet<int>(Enumerable.Range(0, kMessagesPerWorker * senders.Length)));

         while (true) {
            GC.Collect();
         }
      }
开发者ID:the-dargon-project,项目名称:courier,代码行数:54,代码来源:MessagingLoadTests.cs


注:本文中的ConcurrentSet.AddOrThrow方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。