本文整理汇总了C#中BlockingQueue.Take方法的典型用法代码示例。如果您正苦于以下问题:C# BlockingQueue.Take方法的具体用法?C# BlockingQueue.Take怎么用?C# BlockingQueue.Take使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BlockingQueue
的用法示例。
在下文中一共展示了BlockingQueue.Take方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: A_Flow_with_SelectAsyncUnordered_must_not_run_more_futures_than_configured
public void A_Flow_with_SelectAsyncUnordered_must_not_run_more_futures_than_configured()
{
this.AssertAllStagesStopped(() =>
{
const int parallelism = 8;
var counter = new AtomicCounter();
var queue = new BlockingQueue<Tuple<TaskCompletionSource<int>, long>>();
var timer = new Thread(() =>
{
var delay = 500; // 50000 nanoseconds
var count = 0;
var cont = true;
while (cont)
{
try
{
var t = queue.Take(CancellationToken.None);
var promise = t.Item1;
var enqueued = t.Item2;
var wakeup = enqueued + delay;
while (DateTime.Now.Ticks < wakeup) { }
counter.Decrement();
promise.SetResult(count);
count++;
}
catch
{
cont = false;
}
}
});
timer.Start();
Func<Task<int>> deferred = () =>
{
var promise = new TaskCompletionSource<int>();
if (counter.IncrementAndGet() > parallelism)
promise.SetException(new Exception("parallelism exceeded"));
else
queue.Enqueue(Tuple.Create(promise, DateTime.Now.Ticks));
return promise.Task;
};
try
{
const int n = 10000;
var task = Source.From(Enumerable.Range(1, n))
.SelectAsyncUnordered(parallelism, _ => deferred())
.RunAggregate(0, (c, _) => c + 1, Materializer);
task.Wait(TimeSpan.FromSeconds(3)).Should().BeTrue();
task.Result.Should().Be(n);
}
finally
{
timer.Interrupt();
}
}, Materializer);
}