C#4并行计算

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace ConsoleApp1
  8 {
  9     class Program
 10     {
 11         static void Main(string[] args)
 12         {
 13             Parallel.For(0, 15,
 14                 i => Console.WriteLine("The square of {0} is {1}", i, i * i));
 15 
 16             const int maxValues = 50;
 17             int[] squares = new int[maxValues];
 18 
 19             Parallel.For(0, maxValues,
 20                 i => squares[i] = i * i);
 21 
 22             for (var i = 0; i < maxValues; i++)
 23             {
 24                 Console.WriteLine(squares[i]);
 25             }
 26 
 27             Console.WriteLine("==========================================");
 28             string[] words = new string[] { "We", "hold", "there", "to", "be", "self-evident" };
 29 
 30             Parallel.ForEach(words,
 31                 i => Console.WriteLine($"{i} has {i.Length}"));//和For的区别是:For需要索引号,ForEach不需要
 32 
 33             /*
 34             The square of 0 is 0
 35             The square of 6 is 36
 36             The square of 5 is 25
 37             The square of 1 is 1
 38             The square of 4 is 16
 39             The square of 13 is 169
 40             The square of 3 is 9
 41             The square of 11 is 121
 42             The square of 12 is 144
 43             The square of 14 is 196
 44             The square of 2 is 4
 45             The square of 8 is 64
 46             The square of 10 is 100
 47             The square of 9 is 81
 48             The square of 7 is 49
 49             0
 50             1
 51             4
 52             9
 53             16
 54             25
 55             36
 56             49
 57             64
 58             81
 59             100
 60             121
 61             144
 62             169
 63             196
 64             225
 65             256
 66             289
 67             324
 68             361
 69             400
 70             441
 71             484
 72             529
 73             576
 74             625
 75             676
 76             729
 77             784
 78             841
 79             900
 80             961
 81             1024
 82             1089
 83             1156
 84             1225
 85             1296
 86             1369
 87             1444
 88             1521
 89             1600
 90             1681
 91             1764
 92             1849
 93             1936
 94             2025
 95             2116
 96             2209
 97             2304
 98             2401
 99             ==========================================
100             We has 2
101             self-evident has 12
102             to has 2
103             there has 5
104             hold has 4
105             be has 2
106             请按任意键继续. . .
107 
108                          */
109         }
110     }
111 }

 

转载于:https://www.cnblogs.com/wanghaibin/p/10436050.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值