1.主线程参与计算
1 //用Task完成这个 你们会吗 不在意那个主线程参与计算的事儿 2 //Parallel.Invoke(() => this.Coding("爱书客", "Client") 3 // , () => this.Coding("风动寂野", "Portal") 4 // , () => this.Coding("笑看风云", "Service"));
2.2个api,第二个数组是传入的参数(遍历)
1 { 2 Parallel.For(0, 5, i => this.Coding("爱书客", "Client" + i)); 3 } 4 { 5 //Parallel.ForEach(new string[] { "0","1","2","3","4"}, i => this.Coding("爱书客", "Client" + i)); 6 }
3.可以控制并发数量,这里控制线程的最大数量为3,
1 { 2 ////parallelOptions 可以控制并发数量 3 //ParallelOptions parallelOptions = new ParallelOptions(); 4 //parallelOptions.MaxDegreeOfParallelism = 3; 5 //Parallel.For(0, 10, parallelOptions, i => this.Coding("爱书客", "Client" + i)); 6 }