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. Spring异常抛出触发事务回滚

    Spring.EJB的声明式事务默认情况下都是在抛出unchecked exception后才会触发事务的回滚 /** * 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*) ...

  2. Quartz.NET作业调度框架详解(转)

    Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...

  3. java修改图片大小

    import java.awt.Image; import java.awt.image.BufferedImage; import java.io.BufferedInputStream; impo ...

  4. 日志处理之logging模块

    日志级别: 'CRITICAL': CRITICAL, 'ERROR': ERROR, 'WARN': WARNING, 'WARNING': WARNING, 'INFO': INFO, 'DEBU ...

  5. inotifywait命令

    [命令格式]: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ][命令原意]: inote file system wait[命令路径 ...

  6. Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources解决

    解决方法 ruby -v rvm requirements brew install libyaml rvm pkg install openssl rvm install 2.3.1 --with- ...

  7. QT征程之初识qt

    下载 https://www.qt.io/cn/download-open-source/     下载QT离线安装包 Qt 5.5.1 for Linux 32-bit (546 MB) (info ...

  8. Regex

    1. regex with variable example: find the number and put a parenthese around the number. output: a(52 ...

  9. mysql - join two derived tables

    select t1.uid from (select uid from table1) t1 inner join (select uid from table2) t2 where t1.uid=t ...

  10. Winform GDI+ 相关资料

    在Visual Studio 2010中定义GDI+自定义控件——自定义控件介绍 http://www.cnblogs.com/zhangdong/archive/2010/05/20/1740177 ...