本文整理匯總了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);
}