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; }
}