温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

并行Linq

发布时间:2020-07-23 17:54:13 来源:网络 阅读:633 作者:1473348968 栏目:编程语言
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<T>,所以Where()、Select()等方法不在返回IEnumerable<T>,而返回ParallelQuery<T>
            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<int> 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<T>(IEnumerable<T> s)
        {
            Console.WriteLine("==================================");
            foreach (var item in s)
            {
                Console.WriteLine(item);
            }
        }
    }
}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI