zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    linux下自动同步internet时间
    String,StringBuffer与StringBuilder的区别
    Spring Boot CLI安装
    java中Date与String的相互转化
    MyBatis Sql语句中的转义字符
    MyBatis详解 与配置MyBatis+Spring+MySql
    MyBatis的foreach语句详解
    不可变集合 Immutable Collections
    Java日期时间使用总结
    Java将一段逗号分割的字符串转换成一个数组
  • 原文地址:https://www.cnblogs.com/wanghaibin/p/10436050.html
Copyright © 2011-2022 走看看