Parallel 试验

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 = 0; i < 1000; i++)
        {
            var stu = new student();
            stu.age = i;
            stu.id = i;
            studentList.Add(stu);
        }
        List<teacher> teacherList = new List<teacher>();
        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<relation> relationList = new ConcurrentBag<relation>();

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

 

posted on 2016-12-02 09:05  听哥哥的话  阅读(150)  评论(0编辑  收藏  举报

导航