.Net【基础回顾】并行编程Parallel

Parallel三种用法

  • Parallel.Invoke

  • Parallel.for

  • Parallel.forEach

  1. Parallel.Invoke
class Program
{
    static void Main(string[] args)
    {
        var watch = Stopwatch.StartNew();

        watch.Start();

        Run1();

        Run2();

        Console.WriteLine("我是串行开发,总共耗时:{0}\n", watch.ElapsedMilliseconds);

        watch.Restart();

        Parallel.Invoke(Run1, Run2);

        watch.Stop();

        Console.WriteLine("我是并行开发,总共耗时:{0}", watch.ElapsedMilliseconds);

        Console.Read();
    }

    static void Run1()
    {
        Console.WriteLine("我是任务一,我跑了3s");
        Thread.Sleep(3000);
    }

    static void Run2()
    {
        Console.WriteLine("我是任务二,我跑了5s");
        Thread.Sleep(5000);
    }
}
 

Parallel.for

class Program
{
    static void Main(string[] args)
    {
        for (int j = 1; j < 4; j++)
        {
            Console.WriteLine("\n第{0}次比较", j);

            ConcurrentBag<int> bag = new ConcurrentBag<int>();

            var watch = Stopwatch.StartNew();

            watch.Start();

            for (int i = 0; i < 20000000; i++)
            {
                bag.Add(i);
            }

            Console.WriteLine("串行计算:集合有:{0},总共耗时:{1}", bag.Count, watch.ElapsedMilliseconds);

            GC.Collect();

            bag = new ConcurrentBag<int>();

            watch = Stopwatch.StartNew();

            watch.Start();

            Parallel.For(0, 20000000, i =>
            {
                bag.Add(i);
            });

            Console.WriteLine("并行计算:集合有:{0},总共耗时:{1}", bag.Count, watch.ElapsedMilliseconds);

            GC.Collect();

        }
    }
}

Parallel.forEach

class Program
{
    static void Main(string[] args)
    {
        for (int j = 1; j < 4; j++)
        {
            Console.WriteLine("\n第{0}次比较", j);

            ConcurrentBag<int> bag = new ConcurrentBag<int>();

            var watch = Stopwatch.StartNew();

            watch.Start();

            for (int i = 0; i < 3000000; i++)
            {
                bag.Add(i);
            }

            Console.WriteLine("串行计算:集合有:{0},总共耗时:{1}", bag.Count, watch.ElapsedMilliseconds);

            GC.Collect();

            bag = new ConcurrentBag<int>();

            watch = Stopwatch.StartNew();

            watch.Start();

            Parallel.ForEach(Partitioner.Create(0, 3000000), i =>
            {
                for (int m = i.Item1; m < i.Item2; m++)
                {
                    bag.Add(m);
                }
            });

            Console.WriteLine("并行计算:集合有:{0},总共耗时:{1}", bag.Count, watch.ElapsedMilliseconds);

            GC.Collect();

        }
    }
}

设置最大并行数


var cancellationTokenSouce = new CancellationTOkenSource();
ParallelOptions options = new ParallelOptions();
options.MaxDegressOfParallelism = 2; //设置最大并行数,并行的任务有几个
options.CancellationToken = cancellationTokenSouce.token;

Paraller.For(0,100,options,(i,loopstate)=>{
    if(i==100){
        loopstate.Stop();
    }
});

posted on 2022-11-29 22:12  杏村牧童  阅读(101)  评论(0编辑  收藏  举报