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<student> studentList = new List<student>();
for (long i = ; i < ; i++)
{
var stu = new student();
stu.age = i;
stu.id = i;
studentList.Add(stu);
}
List<teacher> teacherList = new List<teacher>();
for (long i = ; i < ; i++)
{
var t = new teacher();
t.id = i;
t.age = i;
teacherList.Add(t);
}
var ha = new Hashtable(); // 262151
Action[] actionList = new Action[];
ConcurrentBag<relation> relationList = new ConcurrentBag<relation>(); long index = ;
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 = ;
}
public long age { get; set; }
} public class teacher
{
internal long id; public teacher()
{
age = ;
}
public long age { get; set; }
}

Parallel 试验的更多相关文章

  1. .Net多线程编程—System.Threading.Tasks.Parallel

    System.Threading.Tasks.Parallel类提供了Parallel.Invoke,Parallel.For,Parallel.ForEach这三个静态方法. 1 Parallel. ...

  2. .net垃圾回收机制编程调试试验

    1. 什么是CLR GC? 它是一个基于引用跟踪和代的垃圾回收器. 从本质上,它为系统中所有活跃对象都实现了一种引用跟踪模式,如果一个对象没有任何引用指向它,那么这个对象就被认为是垃圾对象,并且可以被 ...

  3. Java 8函数编程轻松入门(五)并行化(parallel)

    1.并发与并行的区别 并发: 一个时间段内有几个程序都处于已启动到运行完毕之间,且这几个程序都是在同一个处理机上运行.但在任一个时刻点只有一个程序在处理机上运行 并行: 在同一个时刻,多核处理多个任务 ...

  4. Parallel并行之乱用

    关于Parallel我也不细说了,一则微软封装的很好用,二来介绍这个的遍地都是. 我要说的是,要想成为一个优秀的标题党,一定要把重点放到别的地方,为了节省大家阅读时间,我先把结论说了,然后再慢慢从头说 ...

  5. 代码的坏味道(12)——平行继承体系(Parallel Inheritance Hierarchies)

    坏味道--平行继承体系(Parallel Inheritance Hierarchies) 平行继承体系(Parallel Inheritance Hierarchies) 其实是 霰弹式修改(Sho ...

  6. 多线程之任务: Task 基础, 多任务并行执行, 并行运算(Parallel)

    Task - 基于线程池的任务(在 System.Threading.Tasks 命名空间下) 多 Task 的并行执行 Parallel - 并行计算(在 System.Threading.Task ...

  7. Parallel.Foreach

    随着多核时代的到来,并行开发越来越展示出它的强大威力! 使用并行程序,充分的利用系统资源,提高程序的性能.在.net 4.0中,微软给我们提供了一个新的命名空间:System.Threading.Ta ...

  8. 在Parallel中使用DbSet.Add()发现的一系列多线程问题和解决过程

    发现问题 需求很简单,大致就是要批量往数据库写数据,于是打算用Parallel并行的方式写入,希望能利用计算机多核特性加快程序执行速度.想的很美好,于是快速撸了类似下面的一串代码: using (va ...

  9. Intel.parallel.studio.xe.2015.Update.2.ISO-TBE 下载

    磁力链下载点我 还有linux版本 Intel.parallel.studio.xe.2015.Update.1.LINUX.ISO-TBE 收集自网络,要跨请跨原作者,谢谢.

随机推荐

  1. java类型占用字节数&类型转换

    1.整型类型              存储需求     bit数    取值范围      备注int                 4字节           4*8 short         ...

  2. assert的用法

    assert用来调试时,判断一个语句是否为真. assert是宏,而不是函数.在C的assert.h 头文件中. assert的作用是先计算表达式 expression ,如果其值为假(即为0),那么 ...

  3. C# ADO.NET (sql语句连接方式)(查询)

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  4. linux 学习6 软件包管理 资料链接

    软件包管理 这一章看得比较随意 先大概记点东西. RPM安装很繁琐,因为依赖性,手动安装太麻烦. ______我觉得直接yum在线安装就好了,很直接方便. 但是要特别注意的地方 在用yum升级或者卸载 ...

  5. http://www.cnblogs.com/zhaoguihua/tag/%E9%AB%98%E6%80%A7%E8%83%BD%E7%BD%91%E7%AB%99/

    http://www.cnblogs.com/zhaoguihua/tag/%E9%AB%98%E6%80%A7%E8%83%BD%E7%BD%91%E7%AB%99/

  6. Python 多线程 Condition 的使用

    Condition Condition(条件变量)通常与一个锁关联.需要在多个Contidion中共享一个锁时,可以传递一个Lock/RLock实例给构造方法,否则它将自己生成一个RLock实例. 可 ...

  7. spring的helloworld

    前两天接到了支付宝的电话面试,问了我好多java中的知识,知识姐不看java好多年,这猛一问知道的东西还真是不少啊.趁着现在精力还算是比较旺盛,再把spring重新理一理,为下一次面试做准备. 学习每 ...

  8. linux无线配置

    1. 找 wifi 热点 sudo iwlist scan 2. 生成热点的配置(wpa2加密) sudo wpa_passphrase "wifi热点名称" "wpa密 ...

  9. Sharif University CTF 2016 - Smooth As Silk

    Category: Crypto Points: 200 Solves: 11 Description: p > q n = p*q = 1146153281852525177586999436 ...

  10. 用node搭建静态文件服务器

    占个坑,写个node静态文件服务器