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 总结

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

posted on   BestNow  阅读(650)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示