本文整理汇总了C#中Channel.Get方法的典型用法代码示例。如果您正苦于以下问题:C# Channel.Get方法的具体用法?C# Channel.Get怎么用?C# Channel.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Channel
的用法示例。
在下文中一共展示了Channel.Get方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: should_block_on_get_when_empty
public void should_block_on_get_when_empty()
{
var executionChecker = new OrderedExecutionChecker();
var c = new Channel<int>(1);
var thread = new Thread(() =>
{
Thread.Sleep(100);
Console.WriteLine("[{0}] 1", Thread.CurrentThread.ManagedThreadId);
executionChecker.Executing(1);
c.Put(1);
});
thread.Start();
c.Get();
executionChecker.Executing(2);
thread.Join();
Assert.That(executionChecker.IsInOrder());
}
示例2: should_work_with_many_readers_and_writes
public void should_work_with_many_readers_and_writes()
{
const int numberOfThreads = 50;
const int iteration = 1000;
const int capacity = 100;
var c = new Channel<int>(capacity);
var writes = new List<Thread>();
var readers= new List<Thread>();
var startFlag = new ManualResetEvent(false);
for (int n = 0; n < numberOfThreads; n++)
writes.Add(new Thread(() =>
{
startFlag.WaitOne();
for (int i = 0; i < iteration; i++)
c.Put(0);
}));
for (int n = 0; n < numberOfThreads; n++)
readers.Add(new Thread(() =>
{
startFlag.WaitOne();
for (int i = 0; i < iteration; i++)
c.Get();
}));
foreach (var t in readers) t.Start(); foreach (var t in writes) t.Start();
startFlag.Set();
foreach (var t in readers) t.Join(); foreach (var t in writes) t.Join();
}
示例3: should_produce_and_consume_the_channel_in_order
public void should_produce_and_consume_the_channel_in_order()
{
const int iteration = 100000;
const int capacity = 100;
var c = new Channel<int>(capacity);
var startFlag = new ManualResetEvent(false);
var t1 = new Thread(() =>
{
startFlag.WaitOne();
for (int i = 0; i < iteration; i++)
c.Put(i);
});
var t2 = new Thread(() =>
{
startFlag.WaitOne();
for (int i = 0; i < iteration; i++)
Assert.AreEqual(c.Get().Item, i);
});
t1.Start(); t2.Start();
startFlag.Set();
t1.Join(); t2.Join();
}
示例4: should_get_same_object_as_put
public void should_get_same_object_as_put()
{
var c = new Channel<int>(1);
c.Put(1);
Assert.AreEqual(1, c.Get().Item);
}