zoukankan      html  css  js  c++  java
  • C#中多线程相关1

    分段用线程执行

            private int x= 0;
            private static Object thisLock = new Object();
            private System.Diagnostics.Stopwatch sw = null;
            private void handleData(List<HisAlarm> almList,int count)
            {
                while (x < count)
                {
                    SaveAlarmCache(almList[x]);
                    lock (thisLock)
                    { 
                        x++;
                    }
                    
                    Thread.Sleep(0);
                }
                sw.Stop();
                TimeSpan ts2 = sw.Elapsed;
                Console.WriteLine("sw总共花费{0}ms.", ts2.TotalMilliseconds);
            }
    
            private void handleData2(List<HisAlarm> almList, int count,int index)
            {
                Console.WriteLine("index:{0}",index);
                int len0 = 0;
                int len1 = count / 5;
                int len2 = 2 * (count / 5);
                int len3 = 3 * (count / 5);
                int len4 = 4 * (count / 5);
                switch (index)
                {
                    case 0:
                        {
                            System.Diagnostics.Stopwatch sw0 = new System.Diagnostics.Stopwatch();
                            sw0.Start();
                            while (len0 < len1)
                            {
                                SaveAlarmCache(almList[len0]);
                                len0++;
                            }
                            sw0.Stop();
                            TimeSpan ts0 = sw0.Elapsed;
                            Console.WriteLine("sw0总共花费{0}ms.{1}", ts0.TotalMilliseconds,DateTime.Now.ToString());
                        }
                        break;
                    case 1:
                        {
                            System.Diagnostics.Stopwatch sw1 = new System.Diagnostics.Stopwatch();
                            sw1.Start();
                            while (len1 < len2)
                            {
                                SaveAlarmCache(almList[len1]);
                                len1++;
                            }
                            sw1.Stop();
                            TimeSpan ts1 = sw1.Elapsed;
                            Console.WriteLine("sw1总共花费{0}ms.{1}", ts1.TotalMilliseconds, DateTime.Now.ToString());
                        }
                        break;
                    case 2:
                        {
                            System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();
                            sw2.Start();
                            while (len2 < len3)
                            {
                                SaveAlarmCache(almList[len2]);
                                len2++;
                            }
                            sw2.Stop();
                            TimeSpan ts2 = sw2.Elapsed;
                            Console.WriteLine("sw2总共花费{0}ms.{1}", ts2.TotalMilliseconds, DateTime.Now.ToString());
                        }
                        break;
                    case 3:
                        {
                            System.Diagnostics.Stopwatch sw3 = new System.Diagnostics.Stopwatch();
                            sw3.Start();
                            while (len3 < len4)
                            {
                                SaveAlarmCache(almList[len3]);
                                len3++;
                            }
                            sw3.Stop();
                            TimeSpan ts3 = sw3.Elapsed;
                            Console.WriteLine("sw3总共花费{0}ms.{1}", ts3.TotalMilliseconds, DateTime.Now.ToString());
                        }
                        break;
                    case 4:
                        {
                            System.Diagnostics.Stopwatch sw4 = new System.Diagnostics.Stopwatch();
                            sw4.Start();
                            while (len4 < count )
                            {
                                SaveAlarmCache(almList[len4]);
                                len4++;
                            }
                            sw4.Stop();
                            TimeSpan ts4 = sw4.Elapsed;
                            Console.WriteLine("sw4总共花费{0}ms.{1}", ts4.TotalMilliseconds, DateTime.Now.ToString());
                        }
                        break;
                    default:
                        break;
                }
            }
              var sw = new System.Diagnostics.Stopwatch();
                    sw.Start();
                    foreach (HisAlarm alm in almList)
                    {
                        SaveAlarmCache(alm);
                        //LogHelper.GetLog("Server").Debug("Add Alarm:" + alm);
                    }
    
                    TimeSpan ts2 = sw.Elapsed;
                    Console.WriteLine("sw总共花费{0}ms.", ts2.TotalMilliseconds);
    
    
              //int len = almList.Count;
                    //ParallelLoopResult result = Parallel.For(0, len,  i =>{ SaveAlarmCache(almList[i]);});
    
                    //Parallel.ForEach(almList, new ParallelOptions { MaxDegreeOfParallelism = 100 }, (alm) => { SaveAlarmCache(alm); });
    
                    for (int cnt = 0; cnt < 5; cnt++)
                    {
                        int index = cnt;
                        Thread th = new Thread(() => { handleData2(almList, almList.Count, index); });
                        th.Start();
                    }
    
                    //ParallelLoopResult result = Parallel.ForEach<HisAlarm>(almList, (alm) =>
                    //{
                    //    SaveAlarmCache(alm);
                    //    Thread.Sleep(1); ;
                    //});
                    //LogHelper.GetLog("Server").Debug("ParallelLoopResult:" + result.IsCompleted);
                    //Console.WriteLine("ParallelLoopResult:{0}", result.IsCompleted);
                    //th.Start();
                    //sw.Stop();

            private int x= 0;        private static Object thisLock = new Object();        private System.Diagnostics.Stopwatch sw = null;        private void handleData(List<HisAlarm> almList,int count)        {            while (x < count)            {                SaveAlarmCache(almList[x]);                lock (thisLock)                {                     x++;                }                                Thread.Sleep(0);            }            sw.Stop();            TimeSpan ts2 = sw.Elapsed;            Console.WriteLine("sw总共花费{0}ms.", ts2.TotalMilliseconds);        }
            private void handleData2(List<HisAlarm> almList, int count,int index)        {            Console.WriteLine("index:{0}",index);            int len0 = 0;            int len1 = count / 5;            int len2 = 2 * (count / 5);            int len3 = 3 * (count / 5);            int len4 = 4 * (count / 5);            switch (index)            {                case 0:                    {                        System.Diagnostics.Stopwatch sw0 = new System.Diagnostics.Stopwatch();                        sw0.Start();                        while (len0 < len1)                        {                            SaveAlarmCache(almList[len0]);                            len0++;                        }                        sw0.Stop();                        TimeSpan ts0 = sw0.Elapsed;                        Console.WriteLine("sw0总共花费{0}ms.{1}", ts0.TotalMilliseconds,DateTime.Now.ToString());                    }                    break;                case 1:                    {                        System.Diagnostics.Stopwatch sw1 = new System.Diagnostics.Stopwatch();                        sw1.Start();                        while (len1 < len2)                        {                            SaveAlarmCache(almList[len1]);                            len1++;                        }                        sw1.Stop();                        TimeSpan ts1 = sw1.Elapsed;                        Console.WriteLine("sw1总共花费{0}ms.{1}", ts1.TotalMilliseconds, DateTime.Now.ToString());                    }                    break;                case 2:                    {                        System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();                        sw2.Start();                        while (len2 < len3)                        {                            SaveAlarmCache(almList[len2]);                            len2++;                        }                        sw2.Stop();                        TimeSpan ts2 = sw2.Elapsed;                        Console.WriteLine("sw2总共花费{0}ms.{1}", ts2.TotalMilliseconds, DateTime.Now.ToString());                    }                    break;                case 3:                    {                        System.Diagnostics.Stopwatch sw3 = new System.Diagnostics.Stopwatch();                        sw3.Start();                        while (len3 < len4)                        {                            SaveAlarmCache(almList[len3]);                            len3++;                        }                        sw3.Stop();                        TimeSpan ts3 = sw3.Elapsed;                        Console.WriteLine("sw3总共花费{0}ms.{1}", ts3.TotalMilliseconds, DateTime.Now.ToString());                    }                    break;                case 4:                    {                        System.Diagnostics.Stopwatch sw4 = new System.Diagnostics.Stopwatch();                        sw4.Start();                        while (len4 < count )                        {                            SaveAlarmCache(almList[len4]);                            len4++;                        }                        sw4.Stop();                        TimeSpan ts4 = sw4.Elapsed;                        Console.WriteLine("sw4总共花费{0}ms.{1}", ts4.TotalMilliseconds, DateTime.Now.ToString());                    }                    break;                default:                    break;            }                                }

  • 相关阅读:
    java 读取src下的配置文件
    图书管理系统
    StaticDemo
    MatixDemo
    Magics
    GetPrime
    ConWithThis
    TestTHIS
    ConstructorOverLoad
    Sequence
  • 原文地址:https://www.cnblogs.com/mathyk/p/11233485.html
Copyright © 2011-2022 走看看