我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

并行Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Threading;
using System.Diagnostics;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            //并行LINQ
            //******************************************************
            //测试时间
            Stopwatch sw = Stopwatch.StartNew();
            long l = sw.ElapsedMilliseconds;
            //******************************************************
 
            const int maxsize = 100000000;
            var data = new int[maxsize];
            Random ran = new Random();
            for (int i = 0; i < maxsize; i++)
            {
                checked
                {
                    data[i] = ran.Next(40);
                }
            }
            //===========================================并行查询
            //AsParallel() 启用查询的并行化
            //AsParallel() 返回ParallelQuery,所以Where()、Select()等方法不在返回IEnumerable,而返回ParallelQuery
            var query = (from r in data.AsParallel()
                         select r).Take(20).Select(r => r);
            Foreach(query);
            Console.WriteLine(data.AsParallel().Where(r => r < 20).Sum());

            //===========================================分区器
            List il = Enumerable.Range(0, 10000).ToList();
            //WithExecutionMode(ParallelExecutionMode.ForceParallelism) //强制并行化整个查询
            //WithDegreeOfParallelism() //指定最大任务数
            var query2 = (from r in Partitioner.Create(il, true).AsParallel().WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                          where r < 20
                          select r).Sum();
            Console.WriteLine("====================================");
            Console.WriteLine(query2);
            Console.WriteLine("====================================");
            //===========================================取消长时间运行的任务
            var token = new CancellationTokenSource();
            new Thread(() =>
            {
                try
                {
                    var query3 = (from r in il.AsParallel().WithCancellation(token.Token)
                                  where r < 20
                                  select r
                                    ).Sum();
                    Console.WriteLine(query3);
                }
                catch (Exception e) { Console.WriteLine(e); }
            }).Start();
            if (Console.ReadLine() == "y" || Console.ReadLine() == "Y")
            {
                token.Cancel();
            }

            Console.ReadKey();
        }
        public static void Foreach(IEnumerable s)
        {
            Console.WriteLine("==================================");
            foreach (var item in s)
            {
                Console.WriteLine(item);
            }
        }
    }
}

标题名称:并行Linq
网站网址:http://mswzjz.cn/article/psogsd.html

其他资讯