博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Parallel 试验
阅读量:6239 次
发布时间:2019-06-22

本文共 2566 字,大约阅读时间需要 8 分钟。

using System;using System.Collections.Concurrent;using System.Collections.Generic;using System.Diagnostics;using System.Threading;using System.Threading.Tasks;using System.Linq;using System.Collections;class CQ_EnqueueDequeuePeek{    public readonly static object lockObj = new object();    static void Main()    {        //int n=1000;        //var s1 = n + n * (n - 1) / 2;        //Console.WriteLine(s1);        //Console.WriteLine(1000*1000-s1);        //Console.ReadKey();        //return ;        CancellationTokenSource tokenSource = new CancellationTokenSource();        CancellationToken token = tokenSource.Token;        List
studentList = new List
(); for (long i = 0; i < 1000; i++) { var stu = new student(); stu.age = i; stu.id = i; studentList.Add(stu); } List
teacherList = new List
(); for (long i = 0; i < 1000; i++) { var t = new teacher(); t.id = i; t.age = i; teacherList.Add(t); } var ha = new Hashtable(); // 262151 Action[] actionList = new Action[1000000]; ConcurrentBag
relationList = new ConcurrentBag
(); long index = 0; foreach (var stu in studentList) { foreach (var t in teacherList) { actionList[index] = () => { relation re = new relation(); var age1 = stu.age; var age2 = t.age; re.age1 = age1; re.age2 = age2; re.value = age1 * age2; relationList.Add(re); lock (ha) { if (!ha.ContainsKey(re.value)) { ha.Add(re.value, re.value); } } }; index++; } } Stopwatch s = new Stopwatch(); s.Start(); Parallel.ForEach(actionList, (a) => { a(); }); s.Stop(); Console.WriteLine("总运行时间:" + s.Elapsed + "\r\n"); Console.WriteLine("运行结果不重复的有如下个:\r\n"); Console.WriteLine(relationList.Select(x => x.value).Distinct().LongCount()); Console.WriteLine("运行结果以及表达式如下:\r\n"); Console.WriteLine(string.Join("\r\n", relationList.Select(x => new { str = "age1:" + x.age1 + " age2:" + x.age2 + "=" + x.value }).Distinct())); Console.WriteLine("运行数据大小:" + relationList.Count); Console.ReadKey(); }}public class relation{ public long age1; public long age2; public long value { get; set; }}public class student{ internal long id; public student() { age = 10; } public long age { get; set; }}public class teacher{ internal long id; public teacher() { age = 100; } public long age { get; set; }}

 

转载于:https://www.cnblogs.com/kexb/p/6124420.html

你可能感兴趣的文章
apache日志记录分析
查看>>
COM2 --- 小例子
查看>>
Cisco 交换机 升级 IOS
查看>>
火狐4浏览器动态下载统计背后的SQL技术
查看>>
VMDK虚拟硬盘文件丢失,虚拟机无法启动的解决办法
查看>>
S5PV210-arm-裸机-i2c
查看>>
oracle linux 5.8安装oracle 11g rac 第一章:配置实验环境
查看>>
浏览器: Internet Explorer 7 快捷键
查看>>
【Exchange 2019 設置技巧】修改默認附件大小
查看>>
Centos 6.9中 http-2.2 中的一些基本操作和 https 的实现
查看>>
使用QueryTables生成Excel数据时发生错误
查看>>
活动目录实战之十 多台windows 2003 活动目录至win 2008 r2迁移实战
查看>>
我比我的领导差在哪
查看>>
Spring学习笔记二
查看>>
centos自带的日志切割工具 --- logrotate
查看>>
Java中final和static关键字总结
查看>>
一个故障印发的醒悟
查看>>
vim的日常操作方法
查看>>
Windows7系统安装Oracle数据库图文教程详解
查看>>
我的友情链接
查看>>