Bluesongsong 2017-05-04 07:21 采纳率: 0%
浏览 5445

C# 多线程,记录每个线程运行时间

本人菜鸟,在校学生。
最近在学习多线程技术,实现了一个求解数组中最小值的方法,但是在测试多线程与单线程的计算速度时,却不知道那个才是多线程的运行时间(尴尬到窒息!)
多线程求数组中最小值思想:将一个数组分为两个等大的子数组,并新建两个子线程分别求解每个子数组中的最小值,保存在一个全局变量中。
源程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Diagnostics;

namespace Multithread_PSO
{
        class Program
        {
        //定义锁
                private static readonly object locker = new object();
                static void Main(string[] args)
                {
            //元素个数
                        int EleCounts = 100000;
            //待处理数组
                        int[] MyArr = new int[EleCounts];
            //前半段数组
                        int[] TempArr1 = new int[EleCounts / 2];
            //后半段数组
                        int[] TempArr2 = new int[EleCounts / 2];

                    for (int i = 0; i < EleCounts; i++)
                    {
                            int iSeed = DateTime.Now.Millisecond;
                            Random rd = new Random(iSeed+i);
            //随机给数组赋值
                            MyArr[i] = rd.Next(EleCounts);
            //将数组拆分为两个大小相等的数组
                            if(i<EleCounts/2)
                            {
                                    TempArr1[i] = MyArr[i];
                            }
                            else
                            {
                                    TempArr2[i - EleCounts / 2] = MyArr[i];
                            }
                    }
                    //主线程
                    FindMinElement(MyArr);
                    //新建两个线程th1,th2
                    Thread th1 = new Thread(new ParameterizedThreadStart(thFindMinElement));
                    th1.Name = "子线程1";
                    Thread th2 = new Thread(new ParameterizedThreadStart(thFindMinElement));
                    th2.Name = "子线程2";

                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    th1.Start(TempArr1);
                    //th1.Join();
                    th2.Start(TempArr2);
                    //th2.Join();
                    sw.Stop();
                    TimeSpan ts2 = sw.Elapsed;
                    Console.WriteLine("多线程获得最小值为: {0}, 计时器3共耗时:{1}/ms!\n", MinValue, ts2.TotalMilliseconds);

                    Console.ReadKey();
            }
            public static void thFindMinElement(object DivMyArr)
            {
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    string ThNumber = Thread.CurrentThread.ManagedThreadId.ToString();
                    int[] MyArr = (int[])DivMyArr;
                    if (IsFirstRun)
                    {
                            MinValue = MyArr[0];
                            IsFirstRun = false;
                    }

                    for (int i = 1; i < MyArr.Length; i++)
                    {
                            if (MinValue > MyArr[i])
                                    MinValue = MyArr[i];
                    }
                    sw.Stop();
                    TimeSpan ts2 = sw.Elapsed;
                    Console.WriteLine("当前线程名称(ID)为:{1} ({0}),最小值为:{2},共耗时:{3}/ms!\n", ThNumber, Thread.CurrentThread.Name, MinValue, ts2.TotalMilliseconds);
            }
            public static void FindMinElement(int[] DivMyArr)
            {
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    string ThNumber = Thread.CurrentThread.ManagedThreadId.ToString();
                    int[] MyArr = DivMyArr;
                    MinValue = MyArr[0];
                    for (int i = 1; i < MyArr.Length; i++)
                    {
                            if (MinValue > MyArr[i])
                                    MinValue = MyArr[i];
                    }
                    sw.Stop();
                    TimeSpan ts2 = sw.Elapsed;
                    Console.WriteLine("主线程ID为:{0},最小值为: {1}, 共耗时:{2}/ms!\n", ThNumber, MinValue, ts2.TotalMilliseconds);
            }

            private static int minValue;
            private static bool IsFirstRun = true;
            private static int MinValue
            {
                    get
                    {
                            lock (locker)
                            {
                                    return minValue;
                            }
                    }
                    set { minValue = value; }
            }
    }

}

问题1:
如果不添加 th1.Join()和 th2.Join(),运行结果如下,请问计时器3是多线程运行时间吗?

图片说明

问题2:
如果添加 th1.Join()和 th2.Join(),运行结果如下,请问计时器3是多线程运行时间吗?

图片说明

问题3:
如果均不是,那该怎么计算多线程的总运行时间呢,还望大神不吝赐教!!!

  • 写回答

2条回答

  • Away-Far 2017-05-05 03:23
    关注

    添加 Join()会阻止其它线程的运行,这样就失去了多线程的意义了

    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗