zoukankan      html  css  js  c++  java
  • .NET4中多线程并行方法Parallel.ForEach

    原文发布时间为:2011-12-10 —— 来源于本人的百度文章 [由搬家工具导入]

    namespace ForEachDemo
    {
        using System;
        using System.IO;
        using System.Threading;
        using System.Threading.Tasks;
        using System.Linq;
        using System.Diagnostics;

        class SimpleForEach
        {
            static void Main()
            {
                // A simple source for demonstration purposes. Modify this path as necessary.
                var watch = new Stopwatch();
                var list = Enumerable.Range(0, 10);

                //  Method signature: Parallel.ForEach(IEnumerable<TSource> source, Action<TSource> body)
                watch.Start();
                Parallel.ForEach(list, item =>
                {
                    try
                    {
                        var t = 2 / item;
                        Console.WriteLine("Processing {0} on thread {1}", item,
                                            Thread.CurrentThread.ManagedThreadId);
                    }
                    catch
                    {

                    }

                } //close lambda expression
                     ); //close method invocation

                watch.Stop();
                Console.WriteLine(watch.ElapsedMilliseconds);
                // Keep the console window open in debug mode.
                Console.WriteLine("Processing complete. Press any key to exit.");
                Console.ReadKey();
            }
        }
    }

  • 相关阅读:
    OC之runtime面试题(一)
    OC之runtime的(isKindOfClass和isMemberOfClass)
    OC之runtime(super)
    OC中的__block修饰符
    iOS录音及播放
    webpack5升级过程遇到的一些坑?
    (转)iOS工具--CocoaPods 安装使用总结
    iOS学习--NSObject详解
    iOS学习--通过ipa包如何获取图片资源
    ‘A downloaded software component is corrupted and will not be used. ‘ while publish an iOS app to apple store via Xcode
  • 原文地址:https://www.cnblogs.com/handboy/p/7182568.html
Copyright © 2011-2022 走看看