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


C# Channel.Put方法代码示例

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


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

示例1: Main

        static void Main(string[] args)
        {
            Channel<string> channel = new Channel<string>();

            for (int i = 0; i < 2; i++)
            {
                new Thread((p) =>
                    {
                        int n = (int)p;
                        Random random = new Random();
                        for (int j = 0; true; j++)
                        {
                            string item = string.Concat(n, ":", j);
                            Console.WriteLine("{0} putting {1}", Thread.CurrentThread.Name, item);
                            channel.Put(item);
                            Thread.Sleep(random.Next(1500, 3000));
                        }
                    }) { Name = "Put " + i }.Start(i);
            }

            for (int i = 0; i < 4; i++)
            {
                new Thread(() =>
                    {
                        while (true)
                        {
                            Console.WriteLine("{0} taking", Thread.CurrentThread.Name);
                            string item = channel.Take();
                            Console.WriteLine("{0} took {1}", Thread.CurrentThread.Name, item);
                        }
                    }) { Name = "Take " + i }.Start();
            }
        }
开发者ID:BleuM937,项目名称:ConcurrentLibrary,代码行数:33,代码来源:ChannelTest.cs

示例2: 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();
        }
开发者ID:mtmk,项目名称:cschan,代码行数:26,代码来源:ChannelTests.cs

示例3: should_block_on_put_when_full

        public void should_block_on_put_when_full()
        {
            var executionChecker = new OrderedExecutionChecker();
            var c = new Channel<int>(1);
            c.Put(1);

            var thread = new Thread(() =>
                                        {
                                            Thread.Sleep(100);
                                            Console.WriteLine("[{0}] 1", Thread.CurrentThread.ManagedThreadId);
                                            executionChecker.Executing(1);
                                            c.Get();
                                        });
            thread.Start();

            c.Put(1);
            executionChecker.Executing(2);

            thread.Join();

            Assert.That(executionChecker.IsInOrder());
        }
开发者ID:mtmk,项目名称:cschan,代码行数:22,代码来源:ChannelTests.cs

示例4: putData

 public static void putData(Channel<String> dataChannel)
 {
     while (true)
     {
         Thread.Sleep(DELAY_SECONDS);
         Console.WriteLine(Thread.CurrentThread.Name + " about to put data in channel");
         dataChannel.Put(DATA_STRING);
     }
 }
开发者ID:rlgod,项目名称:C-SharpConcurrencyUtils,代码行数:9,代码来源:Program.cs

示例5: 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();
        }
开发者ID:mtmk,项目名称:cschan,代码行数:32,代码来源:ChannelTests.cs

示例6: 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);
 }
开发者ID:mtmk,项目名称:cschan,代码行数:6,代码来源:ChannelTests.cs


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