zoukankan      html  css  js  c++  java
  • C#多线程学习

      1 问题

      最近工作中,对于性能问题要求比较高,原来的直接取数据,变为需要用多线程来取数据,为实现快速查询。以一个简单的例子来说明下,多线程处理数据的整个流程。

      对一个List数据求和为问题模型。分别比较开多线程和不开多线程的结果。

      代码如下

      2 实现

      (1)、Program类

    // ***********************************************************************
    // Assembly         : Share.Code.MultiThreadTest
    // Author           : Amy
    // Created          : 07-15-2014
    //
    // Last Modified By : Amy
    // Last Modified On : 07-18-2014
    // ***********************************************************************
    // <copyright file="Program.cs" company="Microsoft">
    //     Copyright (c) Microsoft. All rights reserved.
    // </copyright>
    // <summary></summary>
    // ***********************************************************************
    
    using System;
    using System.Collections.Generic;
    
    namespace Share.Code.MultiThreadTest
    {
        /// <summary>
        /// Program
        /// </summary>
        class Program
        {
            /// <summary>
            /// 入口
            /// </summary>
            /// <param name="args">参数</param>
            static void Main(string[] args)
            {
                List<long> list = new List<long>();
                
                for (int i = 0; i < 10000000; i++)
                {
                    list.Add(i);
                }
    
                DateTime start = DateTime.Now;
                DateTime end = DateTime.Now;
                long sum = 0;
                for (int i = 0; i < list.Count; i++)
                {
                    sum += list[i];
                }
    
                end = DateTime.Now;
                Console.WriteLine("主线程结果:" + sum + ",耗时:" + ((TimeSpan)(end - start)).TotalMilliseconds);
    
                start = DateTime.Now;
                SumMultiThread thread = new SumMultiThread(list, 2);
                thread.Execute();
                end = DateTime.Now;
                Console.WriteLine("多线程结果是:" + thread.Result + ",耗时:" + ((TimeSpan)(end - start)).TotalMilliseconds);
    
                Console.Read();
            }
        }
    }

      (2)、SumMultiThread类

    // ***********************************************************************
    // Assembly         : Share.Code.MultiThreadTest
    // Author           : Amy
    // Created          : 07-18-2014
    //
    // Last Modified By : Amy
    // Last Modified On : 07-18-2014
    // ***********************************************************************
    // <copyright file="SumMultiThread.cs" company="Microsoft">
    //     Copyright (c) Microsoft. All rights reserved.
    // </copyright>
    // <summary></summary>
    // ***********************************************************************
    
    using System;
    using System.Collections.Generic;
    using System.Threading;
    
    namespace Share.Code.MultiThreadTest
    {
        /// <summary>
        /// 利用多线程求和
        /// </summary>
        public class SumMultiThread
        {
            /// <summary>
            /// 通知一个线程或多个线程正在发生的事情
            /// </summary>
            private List<ManualResetEvent> manualEventList = new List<ManualResetEvent>();
    
            /// <summary>
            /// 要处理的数据
            /// </summary>
            private List<long> data = new List<long>();
    
            /// <summary>
            /// 子线程结果
            /// </summary>
            private List<long> subThreadResult = new List<long>();
    
            /// <summary>
            /// 子线程数量
            /// </summary>
            private int subThreadCount;
    
            /// <summary>
            /// 构造方法
            /// </summary>
            /// <param name="data">要处理的数据</param>
            /// <param name="subThreadCount">线程的个数</param>
            public SumMultiThread(List<long> data, int subThreadCount)
            {
                this.data = data;
                if (subThreadCount >= 1)
                {
                    this.subThreadCount = subThreadCount;
                }
                else
                {
                    this.subThreadCount = 1;
                }
            }
    
            /// <summary>
            /// 处理结果
            /// </summary>
            public long Result
            {
                get;
                set;
            }
    
            /// <summary>
            /// 执行方法
            /// </summary>
            public void Execute()
            {
                for (int i = 0; i < this.subThreadCount; i++)
                {
                    ManualResetEvent mre = new ManualResetEvent(false);
                    this.manualEventList.Add(mre);
                    ThreadPool.QueueUserWorkItem(this.ThreadMethod, i);
                }
    
                WaitHandle.WaitAll(this.manualEventList.ToArray());
    
                // 把每个线程的计算结果加起来
                long sum = 0;
                for (int i = 0; i < this.subThreadResult.Count; i++)
                {
                    sum += this.subThreadResult[i];
                }
    
                this.Result = sum;
            }
    
            /// <summary>
            /// 线程方法
            /// </summary>
            /// <param name="obj">对象</param>
            private void ThreadMethod(object obj)
            {
                DateTime start = DateTime.Now;
    
                DateTime end = DateTime.Now;
                int index = (int)obj;
                int dataPerThread = 0;
                if (this.data != null && this.data.Count > 0)
                {
                    dataPerThread = this.data.Count / this.subThreadCount;
                }
    
                long sum = 0;
    
                if (index == (this.subThreadCount - 1))
                {
                    // 最后一个线程要
                    sum = 0;
                    for (int i = index * dataPerThread; i < this.data.Count; i++)
                    {
                        sum += this.data[i];
                    }
                }
                else
                {
                    // 其他线程
                    for (int i = index * dataPerThread; i < (index * dataPerThread) + dataPerThread && i < this.data.Count; i++)
                    {
                        sum += this.data[i];
                    }
                }
    
                this.subThreadResult.Add(sum);
                end = DateTime.Now;
                Console.WriteLine("线程" + index + "执行完毕,耗时" + ((TimeSpan)(end - start)).TotalMilliseconds);
                
                this.manualEventList[index].Set();
            }
        }
    }

     3 结果

    (1)、两个线程的对比

    (2)、三个线程的对比

    (3)、4个线程的对比

    (4)、5个线程的对比

    4 总结

      在某些情况下,用多线程来提高速度,但是线程的数量也许考量。

    作者:BestNow
    出处:http://www.cnblogs.com/BestNow/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    解决PKIX:unable to find valid certification path to requested target 的问题
    Linux 上的常用文件传输方式介绍与比较
    用VNC远程图形化连接Linux桌面的配置方法
    红帽中出现”This system is not registered with RHN”的解决方案
    linux安装时出现your cpu does not support long mode的解决方法
    CentOS SSH配置
    es6扩展运算符及rest运算符总结
    es6解构赋值总结
    tortoisegit安装、clon、推送
    es6环境搭建
  • 原文地址:https://www.cnblogs.com/tianxue/p/3853986.html
Copyright © 2011-2022 走看看