Parallel.For & Parallel.ForEach & Parallel.Invoke

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace 24
{
    class Program
    {
        static void Main(string[] args)
        {
            //Action[] actions = new Action[] { 
            //new Action(()=>{
            //    Console.WriteLine("hello");
            //}),
            //new Action(()=>{
            //    Console.WriteLine("hello");
            //}),
            //            new Action(()=>{
            //    Console.WriteLine("hello");
            //}),
            //            new Action(()=>{
            //    Console.WriteLine("hello");
            //}),
            //            new Action(()=>{
            //    Console.WriteLine("hello");
            //}),
            //            new Action(()=>{
            //    Console.WriteLine("hello");
            //}),
            //            new Action(()=>{
            //    Console.WriteLine("hello");
            //}),
            //            new Action(()=>{
            //    Console.WriteLine("hello");
            //}),
            //            new Action(()=>{
            //    Console.WriteLine("hello");
            //}),
            //            new Action(()=>{
            //    Console.WriteLine("hello");
            //}),
            //            new Action(()=>{
            //    Console.WriteLine("hello");
            //}),
            //            new Action(()=>{
            //    Console.WriteLine("hello");
            //})
            //};


            //for (int i = 0; i < actions.Length; i++)
            //{
            //    actions[i]();
            //}


            并行循环
            //Parallel.Invoke(actions);


            //List<int> list = new List<int>{ 1, 2, 3, 6, 8, 56, 78, 95, 23, 45, 65, 21, 34, 67, 87 };


            //Stopwatch watch = new Stopwatch();
            //watch.Start();


            Parallel.For(1, 11, i => {
               Console.WriteLine(i + ",线程ID:" + Thread.CurrentThread.ManagedThreadId);
               Thread.Sleep(1000);
            });


            //Parallel.ForEach<int>(list, n => {
            //    Console.WriteLine(n);
            //    Thread.Sleep(1000);
            //});




            //watch.Stop();
            //Console.WriteLine("OK");
            //Console.WriteLine(watch.Elapsed);
            //Console.ReadKey();


            // 产生测试资料
            List<int> testData = new List<int>();
            Random rand = new Random();
            // 产生乱数列表
            for (int i = 0; i < 1000000; i++)
            {
                testData.Add(rand.Next(1000));
                //Console.WriteLine(rand.Next(10));
            }
            // 打印正确结果
            Console.WriteLine(testData.Sum());


            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine();
                TestFor(testData);
                TestParallelFor(testData);
                TestParallelForEach(testData);
            }


           Console.ReadKey();


        }


        static void TestParallelForEach(List<int> testData)
        {
            DateTime time1 = DateTime.Now;
            ConcurrentStack<int> resultData = new ConcurrentStack<int>();
            Parallel.ForEach(testData, (item, loppState) =>
                {
                    item.ToString();
                });
            Console.WriteLine(string.Format("Parallel.ForEach: t{0} in t{1}", testData.Sum(), (DateTime.Now - time1).TotalMilliseconds));
        }




        static void TestParallelFor(List<int> testData)
        {
            DateTime time1 = DateTime.Now;
            Parallel.For(0, testData.Count, (i, loopState) => {
                testData[i].ToString();
            });
            Console.WriteLine(string.Format("Parallel.For: t{0} in  t{1}", testData.Sum(), (DateTime.Now - time1).TotalMilliseconds));
        }
       
        static void TestFor(List<int> testData)
        {
            DateTime time1 = DateTime.Now;
            foreach (var item in testData)
            {
                item.ToString();
            }
            Console.WriteLine(string.Format("ForEach: t{0} in t{1}", testData.Sum(), (DateTime.Now - time1).TotalMilliseconds));
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值