CCR 编写一个不用创建线程,不用考虑资源互斥的多线程程序

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Ccr.Core;
using System.Threading;

namespace ConsoleApplication1
ExpandedBlockStart.gifContractedBlock.gif
{
    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ContractedSubBlock.gifExpandedSubBlockStart.gif            
code#region code
            
int maximumDepth = 10;
            
// step1: 创建一个Dispatcher对象
            Dispatcher dispatcher = new Dispatcher(0"调度器");
            
// step2: 创建一个与step1创建对象关联的DispatcherQueue对象
            DispatcherQueue depthThrottledQueue = new DispatcherQueue("带策略的任务队列",
                                                        dispatcher,
                                                        TaskExecutionPolicy.Unconstrained,
                                                        maximumDepth);
            
// step3: 创建一个Port,能够接收整形数据
            Port<int> intPort = new Port<int>();
            
// step4: 把Port与处理函数关联,然后再与DispatcherQueue关联
            Arbiter.Activate(depthThrottledQueue,
                            Arbiter.Receive(
true,
                                        intPort,
                                        
delegate(int i)    // 这里用了一个匿名方法,作为处理函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
                                        {
                                            Thread.Sleep(
2000);
                                            Console.WriteLine(
"[{0}] {1}", DateTime.Now.ToString("o"), i);
                                        }

                                        )
                            );

            
// step5: 快速的提交大量的任务
            Console.WriteLine("[{0}] 开始提交大量的任务", DateTime.Now.ToString("o"));
            
for (int i = 0; i < maximumDepth * 100000; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                intPort.Post(i);    
// Post到intPort内的数据,会被Receive与处理函数 组合成一个任务,放入depathThrottledQueue
            }

            Console.WriteLine(
"[{0}] 大量任务提交完毕。", DateTime.Now.ToString("o"));

            Console.WriteLine(
"Press any key to exit");
            Console.ReadKey();
            dispatcher.Dispose();
            
#endregion

        }

    }

}

     这次的例子,是一个很简单的控制台,她将面对瞬间提交的百万的数据,而面不改色(CPU、内存非常平稳),队列中始终只保存最新的数据,每次只处理cpu个数据(我的机器是双核的,所以,在我这里,就是每个CPU一个线程,真正的并行运行哦....)

 

     最后,我们要把数据、处理函数、任务队列 组合起来,这就是上面代码中的step4,这步其实做了2个工作:

1、把port和处理函数,封装为Receive关联起来;

2、把Receive和DispatcherQueue关联起来;

 

    这样,我们就完成了,所有的工作,CCR提供了一个模式,让我们只需要把需要并发、异步处理的工作,分解为:

1、输入数据--->post到Port内;

2、处理过程--->做成委托关联到任务队列中

 

例子

中文帮助手则

转载于:https://www.cnblogs.com/sgciviolence/archive/2008/09/10/1288344.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值