Exemple #1
0
        void Dispatch(MessageResult ctx)
        {
            var handlers = Subject.Subscriber.Observers;

            var syncHandlers  = handlers.Where(i => i.Value == SubscribeMode.Sync).Select(i => i.Key).ToArray();
            var asyncHandlers = handlers.Where(i => i.Value == SubscribeMode.Async).Select(i => i.Key).ToArray();

            var handlerCount = syncHandlers.Length + asyncHandlers.Length;
            var barrier      = new AtomicInteger(handlerCount);

            foreach (var item in syncHandlers)
            {
                if (!DoExecute(ctx, item, barrier))
                {
                    break;
                }
            }


            Paraller.ForEach(asyncHandlers, handler => DoExecute(ctx, handler, barrier));
        }
        public void ParallerTest()
        {
            int loopCount   = 1;
            int arrayLength = 10 * 1000 * 1000;

            Console.WriteLine(Environment.ProcessorCount);

            int maxWorkThreads           = 0;
            int maxCompletionPortThreads = 0;

            ThreadPool.GetMaxThreads(out maxWorkThreads, out maxCompletionPortThreads);
            Console.WriteLine(maxWorkThreads);
            Console.WriteLine(maxCompletionPortThreads);


            int minWorkThreads           = 0;
            int minCompletionPortThreads = 0;

            ThreadPool.GetMinThreads(out minWorkThreads, out minCompletionPortThreads);
            Console.WriteLine(minWorkThreads);
            Console.WriteLine(minCompletionPortThreads);

            int workThreads           = 0;
            int completionPortThreads = 0;

            ThreadPool.GetAvailableThreads(out workThreads, out completionPortThreads);
            Console.WriteLine(workThreads);
            Console.WriteLine(completionPortThreads);



            CodeTimer.Time("Paraller.For", loopCount, () =>
            {
                Paraller.For(0, arrayLength, i =>
                {
                    var m = i + 1;
                });
            });

            CodeTimer.Time("For ", loopCount, () =>
            {
                var array = Enumerable.Range(0, arrayLength);
                foreach (var i in array)
                {
                    var m = i + 1;
                }
            });


            var result = new List <int>(10);

            CodeTimer.Time("Sum ", 1, () =>
            {
                Paraller.For(0, 10, i =>
                {
                    Thread.Sleep(i * 10);
                    lock (this)
                        result.Add(i);
                });
            });

            Thread.Sleep(2000);

            result.ForEach(i => Console.WriteLine(i));
            Console.WriteLine(result.Sum());

            var urls = new string[] {
            };


            CodeTimer.Time("Paraller.Urls", loopCount, () =>
            {
                Paraller.For(0, urls.Length, i =>
                {
                    new WebClient().OpenRead(urls[i]);
                    //lock(typeof(Console))
                    Console.WriteLine(urls[i]);
                });
            });

            CodeTimer.Time("urls ", loopCount, () =>
            {
                foreach (var url in urls)
                {
                    new WebClient().OpenRead(url);
                    Console.WriteLine(url);
                }
            });
        }