后端开发|C#.Net教程
.NET,多线程
后端开发-C#.Net教程
A、并行初试:
pc端三级分销源码,ubuntu蓝灯怎么安装,tomcat路径在哪里改,google翻译 爬虫,中间件php框架,seo诞生lzw
public static void test() { for (int i = 0; i < 10000; i++) {Console.WriteLine(i); } } public static void test1() { for (int i = 0; i < 10000; i++) {Console.WriteLine(i + "aaaaaaaaaaaaaaa"); } }
调用:
cgi简单源码,ubuntu手动分区选项,python爬虫43讲,php 钩子解释,discuz seo怎样lzw
static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start();//串行执行:test();test1();//并行执行:17Parallel.Invoke(test, test1);19sw.Stop();21Console.WriteLine("共耗费时间:");23Console.WriteLine(sw.ElapsedMilliseconds / 1000+"s");}
B、分区并行:
商城html模板源码,卸载ubuntu直接删除,tomcat+后台运行命令,在哪写爬虫,php获取所有get数据,修文seolzw
Parallel.ForEach(Partitioner.Create(1,20,5),(x,s)=>{//并行代码中自定义串行,第三个参数表示item1到item2之间的范围Console.WriteLine(x);for (int i = x.Item1; i < x.Item2; i++){ if (i == 10) break; Console.WriteLine(i);}s.Break();// 非常类似普通for循环中的breakif (s.ShouldExitCurrentIteration) return; });
C、异常捕获:AggregateException
int[] arry = new int[10001]; for (int i = 0; i { Console.WriteLine(x); if (sw.Elapsed.Seconds > 3) { throw new TimeoutException("操作超时"); }}); } catch (AggregateException ex) {foreach (var item in ex.InnerExceptions){ Console.WriteLine(item);} }
D、指定并行调度:
ParallelOptions options = new ParallelOptions(); options.MaxDegreeOfParallelism = 1;//如果设置为1就类似于串行代码按顺序执行 options.MaxDegreeOfParallelism =Environment.ProcessorCount;//获取计算机上面的处理器数量 Parallel.For(1,10,options,(x) => {Console.WriteLine(x); });