zoukankan      html  css  js  c++  java
  • 学习笔记——并行编程Parallel

    Parallel 并行运算

    参考资料:http://www.cnblogs.com/woxpp/p/3925094.html

    1.并行运算

    使用Parallel并行运算时,跟task很像,相当于task+waitall  启动多个线程计算  主线程也参与计算,就是节约了一个线程

    Parallel.Invoke(() => this.DoSomethingLong("btnParallel_Click_002"),

        () => this.DoSomethingLong("btnParallel_Click_001"),

        () => this.DoSomethingLong("btnParallel_Click_003"),

        () => this.DoSomethingLong("btnParallel_Click_004"),

        () => this.DoSomethingLong("btnParallel_Click_005"));

         

    2.使用方法:

    2.1.Parallel.For

        eg:Parallel.For(0, 5, t =>

              {

                 this.DoSomethingLong($"btnParallel_Click_00{t}");

              });                  

    2.2.Parallel.Foreach

         eg:Parallel.ForEach(new int[] { 0, 1, 2, 3, 4 }, t =>

               {

                  this.DoSomethingLong($"btnParallel_Click_00{t}");

                });

     2.3.设置并行运算线程数量      

           ParallelOptions options = new ParallelOptions()

              {

                  MaxDegreeOfParallelism = 3

               };

          Parallel.ForEach(new int[] { 0, 1, 2, 3, 4 }, options, t =>

              {

                  this.DoSomethingLong($"btnParallel_Click_00{t}");

              });

     或者

           Parallel.ForEach(new int[] { 0, 1, 2, 3, 4 }, options, (t, state) =>

            {

                this.DoSomethingLong($"btnParallel_Click_00{t}");

                //state.Stop();//结束全部的

                //state.Break();//停止当前的

                //return;

             });  

  • 相关阅读:
    redis该怎么用
    cookie和session的比较
    web常见攻击
    请大神指导从大日志文件中统计关键字次数的办法
    apache中 MaxClients 与MaxRequestsPerChild
    如何提高缓存命中率
    CSU-ACM2018暑假集训比赛1
    CodeForces
    CodeForces
    CodeForces
  • 原文地址:https://www.cnblogs.com/lyyd/p/10059941.html
Copyright © 2011-2022 走看看