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. Foundation ----->NSArray

    .数组的创建     //注意:在OC的数组中,只能够存放对象 //    NSArray *array = [NSArray arrayWithObject:12];错误          //创建 ...

  2. java中获取接口(方法)中的参数名字(eclipse设置编译参数)(java8 javac -parameters)

    interface接口参数 jdk1.7及以前使用spring功能实现的: 注意: 1.该功能只能获取类的方法的参数名,不能获取接口的方法的参数名. public static void test() ...

  3. ubuntu 配置nginx+php+mysql 遇到的一些问题

    /* 公司内网打算配置一台ubuntu为主机的测试服务器.刚好手头有一个昂达的主机,装的windows 声音又大,还不如直接装ubuntu .声音又小,还占用资源少. */ 刚开始安装php5 结果提 ...

  4. jquery 序列化

    //生成发件人Json信息 function buildSenderInfoJson() { var sendName = $("#SendName").val(); var se ...

  5. 正确理解javascript当中的面向对象

    认识面向对象: 为了说明 JavaScript 是一门彻底的面向对象的语言,首先有必要从面向对象的概念着手 , 探讨一下面向对象中的几个概念: 1.万物皆为空:万物皆对象 2.对象具有封装和继承特性 ...

  6. asp.net 对数据库表增加,删除,编辑更新修改

    using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...

  7. CSS overflow 属性

     值 描述 visible 默认值.内容不会被修剪,会呈现在元素框之外. hidden 内容会被修剪,并且其余内容是不可见的. scroll 内容会被修剪,但是浏览器会显示滚动条以便查看其余的内容. ...

  8. redis启用持久化

    redis的持久化有rdb和aof两种. rdb是记录一段时间内的操作,一盘的配置是一段时间内操作超过多少次就持久化. aof可以实现每次操作都持久化. 这里我们使用aof. 配置方式,打开redis ...

  9. [Docker] docker 基础学习笔记3(共6篇)

    首先我们安装好了ssh server之后, 我们需要将这个容器commit,然后启动这个被commit的image. 启动方式: docker run -d -p 2222:22 /usr/sbin/ ...

  10. expect使用demo

    #!/usr/bin/expect set timeout set ip [lindex $argv ] spawn ssh root@$ip expect { "yes/no" ...