TPL

namespace TPLTest

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void button1_Click(object sender, EventArgs e)

        {

            //var list = testFillParallel();

            //int i = list.Count();



            Stopwatch watch = new Stopwatch();

            watch.Start();

            for (int i = 1; i < 5; i++)

            {

                ProcessLongTime(i);

            }

            //watch.Stop();

            this.label1.Text = string.Format("顺序执行用时:" + watch.ElapsedMilliseconds);



            watch.Restart();

            //watch.Start();

            Parallel.For(1, 5, p =>

            {

                ProcessLongTime(p);

                //MessageBox.Show(p.ToString());

            });

            //watch.Stop();

            this.label2.Text = string.Format("并行执行用时:" + watch.ElapsedMilliseconds);



            //watch.Restart();

            ////watch.Start();

            //for (int i = 1; i <= 5; i++)

            //{

            //    Task myTask = new Task(obj => ProcessLongTime((int)obj), i);

            //    myTask.Start();

            //}

            ////watch.Stop();

            //this.label3.Text = string.Format("并行Task执行用时:" + watch.ElapsedMilliseconds);



            watch.Restart();

            int count = 5;

            int results;

            Semaphore semaphore = new Semaphore(0, count);

            System.Threading.Tasks.Parallel.For(0, count, i =>

            {

                results = ProcessLongTimeI(i);

                semaphore.Release();

            });



            //for (var i = 0; i <= count; i++)

            //{

            //    semaphore.WaitOne();

            //    //Console.WriteLine("Got " + i);

            //}

            watch.Stop();

            this.label3.Text = string.Format("并行Task执行用时:" + watch.ElapsedMilliseconds);

        }





        private  IEnumerable<Person> testFillParallel()

        {

            //var list = new List<Person>(9);

            var list = new BlockingCollection<Person>(9);  //必须使用线程安全的集合类型



            Enumerable.Range(1, 999).AsParallel().ForAll(n =>

                {

                    var name = "Person" + n%9;

                    if (list.Count(p => p.Name == name) < 1) list.Add(new Person {Id = n, Name = name});

                });

            this.label1.Text=string.Format("Person's count is {0}", list.Count);

            return list;

        }



        private void ProcessLongTime(int mi)

        {

            for (int i = 0; i < 10000000; i++)

            {

                i++;

                i--;

            }

        }



        private int ProcessLongTimeI(int mi)

        {

            for (int i = 0; i < 10000000; i++)

            {

                i++;

                i--;

            }

            return mi;

        }



        private void button2_Click(object sender, EventArgs e)

        {

            List<int> ls=new List<int>();

            for (int i = 0; i < 5; i++)

            {

                ls.Add(i);

            }



            Stopwatch watch = new Stopwatch();

            watch.Start();

            foreach (int i in ls)

            {

                ProcessLongTime(i);

            }

            this.label1.Text = string.Format("顺序执行用时:" + watch.ElapsedMilliseconds);



            watch.Restart();

            Parallel.ForEach(ls,p=>

            {

                ProcessLongTime(p);

                //MessageBox.Show(p.ToString());

            });

            this.label2.Text = string.Format("并行执行用时:" + watch.ElapsedMilliseconds);



            watch.Restart();

            int count = 5;

            int results;

            Semaphore semaphore = new Semaphore(0, ls.Count);

            System.Threading.Tasks.Parallel.ForEach(ls, p =>

            {

                results = ProcessLongTimeI(p);

                semaphore.Release();

            });



            watch.Stop();

            this.label3.Text = string.Format("并行Task执行用时:" + watch.ElapsedMilliseconds);

        }

    }

 

你可能感兴趣的:(T)