C# 多线程,论多核时代爱恨情仇

开发 后端
老板只有两种,好的和坏的。好的老板只跟你谈钱,坏的老板只跟你谈理想。

博客前言

为什么要学习多线程?

  • 2010年1月21日是10年某市公务员考试的报名截止日。因从下午2点开始,用于报名的北京市人事考试网瘫痪,原定于昨天下午5点截止的报名时间延迟至今天上午11点。

  • 2011年3月11日下午5时(北京时间12日早9点),苹果发布新一代的平板电脑产品iPad 2,配备了A5.1Ghz双核处理器,这寓意着平板电脑和笔记本一同进入"多核时代"。

  • 同年6月18日,国内著名B2C---京东在周年庆典之际举行了"隆重"的大规模的促销活动,抢购随之而来,订单挤爆京东 限时达临时取消。 昨天有消费者反映,由于点击量过大,18日早上京东商城网站一度瘫痪。一位消费者说:“18日凌晨1点开始就登不上京东商城。”刘强东也表示:由于流量多 次超过4个G,服务器运行缓慢。 昨天,京东商城官网发布公告称,“‘618’活动异常火爆且用户下单速度空前,致使部分用户已购订单显示出现延迟,用户在一段时间内无法在‘我的京东’中 查询到自己的订单。目前已购订单显示延迟的问题已得到有效解决,对此给您带来的不便,我们深表歉意。”

  • 2015年05月05日登录风信子网上商城发现,首页除了广告和相关消息外,只有“注册账号获取更多优惠”这唯一一个按钮,没有商品展示,没有产 品搜索,不能网上下单,甚至连进入商城的按钮也没有。风信子南沙跨境商品直购体验中心相关负责人表示,这主要是因为预约的人数太多,截至五一,预约人数已 超过十万,太多人频繁登陆,导致网店服务器瘫痪,目前技术人员还在努力维修中。该负责人介绍,体验中心的网站目前正在调试,“网站目前的作用主要是给市民 预约和提前注册,通过网络注册的市民不用在现场验证身份证等信息,可以提高购买效率。”

下面通过一些实例来认识一下多线程和让大家知道为什么要学习多线程。

[[157023]]

写在前面

老板只有两种,好的和坏的。好的老板只跟你谈钱,坏的老板只跟你谈理想。

v模拟场景

假设后台有个monitor时事的在监测订单,且可以发现订单然后处理订单,每次(1次/S)可以处理1千个订单,需要向仓库系统发出指令,让他们负责配送发货。那么我们来写一个EmulationSystem(模拟系统)

C# 多线程

JobHelper因为我们只是为了模拟一个环境,所以它是这样的。

 

//------------------------------------------------------------------------------ 
// <copyright file="JobHelper.cs" company="CNBlogs Corporation" owner="请叫我头头哥"> 
//     Copyright (C) 2015-2016 All Rights Reserved 
//     原博文地址: http://www.cnblogs.com/toutou/ 
// </copyright>  
//------------------------------------------------------------------------------ 
namespace CNBlogs.Common.Shared 

    using System.Threading; 
    /// <summary> 
    /// Job helper 
    /// </summary> 
    public class JobHelper 
    { 
        /// <summary> 
    /// Get job total count 
        /// </summary> 
    /// <returns></returns> 
    public int GetJobCount() 
        { 
            // 我们的侧重点是多线程,所以我们就模拟每次有1千个订单,而模拟,所以我们不关心其他的。只要订单数量。 
    return 1000
        } 
        /// <summary> 
    /// Submit job 
        /// </summary> 
    /// <param name="jobId">For job id</param> 
    /// <returns>Submit job status</returns> 
    public bool SubmitJob(int jobId) 
        { 
            // 假设针对每个订单向后台发送任务需要1秒,而且每个订单都能成功发送 
            Thread.Sleep(1000); 
            return true
        } 
    } 

  • 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.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.

 

背景我们也交待完了,现在需要来得到这些订单信息以后,向仓库submit这些订单。

v实战演习

根据这种背景,我们可以有很多处理的办法。

  • 传统办法:

    (开启无脑模式:未使用多线程)

    代码正文:

     

    //------------------------------------------------------------------------------ 
    // <copyright file="Runner.cs" company="CNBlogs Corporation" owner="请叫我头头哥"> 
    //     Copyright (C) 2015-2016 All Rights Reserved 
    //     原博文地址: http://www.cnblogs.com/toutou/ 
    // </copyright>  
    //------------------------------------------------------------------------------ 
    namespace CNBlogs.EmulationSystem 
    
        using System; 
        using System.Threading; 
        using CNBlogs.Common.Shared; 
        class Runner 
        { 
            /// <summary> 
                    /// Job helper 
            /// </summary> 
                    private static JobHelper jobHelper = new JobHelper(); 
            /// <summary> 
                    /// Runner lock 
            /// </summary> 
                    private static Mutex mutex; 
            static void Main(string[] args) 
            { 
                // 记录开始处理的时间 
                DateTime paddingTime = DateTime.Now.ToLocalTime(); 
                int orderCount = jobHelper.GetJobCount(); 
                // 用一个指示调用线程是否应拥有互斥体的初始所属权的布尔值来初始化 Mutex 类的新实例。 
                // 当前进程只能启动一次 
                mutex = new Mutex(false"CNBlogs.EmulationSystem"); 
                if (!mutex.WaitOne(0false)) 
                { 
                    Console.Out.WriteLine("Monitor already running..."); 
                    return
                } 
                for (int i = 0; i < orderCount; i++) 
                { 
                    // 假设i就是job id 
                    jobHelper.SubmitJob(i); 
                } 
                // 记录处理完成的时间 
                DateTime completeTime = DateTime.Now.ToLocalTime(); 
                var minutes = (completeTime - paddingTime).TotalSeconds; 
                Console.WriteLine(string.Format("处理{0}个订单,花费时间{1}秒", orderCount, minutes)); 
            } 
        } 
    
    
    • 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.
    • 32.
    • 33.
    • 34.
    • 35.
    • 36.
    • 37.
    • 38.
    • 39.
    • 40.
    • 41.
    • 42.
    • 43.
    • 44.
    • 45.
    • 46.

    PS:现在的这些个电商,动不动来个什么周年庆店庆什么双11双12的一顿突突,搞得咱这些老百姓就全部蜂拥而上,显然如果用传统的方法虽然不会出错,但是老板肯定会找你喝茶。在多核时代用这种方法也只能铁不成钢了。所以这个方法是绝对不可取的。

  • Task方法:

    如果有对Task不是很熟悉的园友可以在这里解开心谜。

    代码正文:

     

    //------------------------------------------------------------------------------ 
    // <copyright file="Runner.cs" company="CNBlogs Corporation" owner="请叫我头头哥"> 
    //     Copyright (C) 2015-2016 All Rights Reserved 
    //     原博文地址: http://www.cnblogs.com/toutou/ 
    // </copyright>  
    //------------------------------------------------------------------------------ 
    namespace CNBlogs.EmulationSystem 
    
        using System; 
        using System.Collections.Generic; 
        using System.Threading; 
        using System.Threading.Tasks; 
        using CNBlogs.Common.Shared; 
        class Runner 
        { 
            /// <summary> 
                    /// Job helper 
            /// </summary> 
                    private static JobHelper jobHelper = new JobHelper(); 
            /// <summary> 
                    /// Runner lock 
            /// </summary> 
                    private static Mutex mutex; 
            static void Main(string[] args) 
            { 
                // 记录开始处理的时间 
                DateTime paddingTime = DateTime.Now.ToLocalTime(); 
                int orderCount = jobHelper.GetJobCount(); 
                // 用一个指示调用线程是否应拥有互斥体的初始所属权的布尔值来初始化 Mutex 类的新实例。 
                // 当前进程只能启动一次 
                mutex = new Mutex(false"CNBlogs.EmulationSystem"); 
                if (!mutex.WaitOne(0false)) 
                { 
                    Console.Out.WriteLine("Monitor already running..."); 
                    return
                } 
                var taskList = new List<Task>(); 
                for (int i = 0; i < orderCount; i++) 
                { 
                    int temp=i; 
                    taskList.Add(Task.Factory.StartNew(() => 
                    { 
                        // 假设i就是job id 
                        jobHelper.SubmitJob(temp); 
                    })); 
                } 
                // 等待所有task执行完毕 
                Task.WaitAll(taskList.ToArray()); 
                // 记录处理完成的时间 
                DateTime completeTime = DateTime.Now.ToLocalTime(); 
                var minutes = (completeTime - paddingTime).TotalSeconds; 
                Console.WriteLine(string.Format("Complete: {0},cost {1} s", orderCount, minutes)); 
            } 
        } 
    
    
    • 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.
    • 32.
    • 33.
    • 34.
    • 35.
    • 36.
    • 37.
    • 38.
    • 39.
    • 40.
    • 41.
    • 42.
    • 43.
    • 44.
    • 45.
    • 46.
    • 47.
    • 48.
    • 49.
    • 50.
    • 51.
    • 52.
    • 53.
    • 54.
    • 55.

     

    代码效果:

    C# 多线程

    相信分别从有TASK和没有TASK的这两次demo中,可以清楚的发现多线程处理这些频繁交互能力的魅力。你会爱上这个方法。确实提高的效率。但是问题也随之而来,一个进程多线程的总数和大小是有要求的(这个取决于服务器的配置),不是任由我们肆意的开采的。如果订单太多,我们不控制task那是会拉仇恨的。task需要控制。

    C# 多线程

  • Semaphore:

    如果有对Semaphore不是很熟悉的园友可以在这里解开心谜。

    代码正文: 

    //------------------------------------------------------------------------------ 
    // <copyright file="Runner.cs" company="CNBlogs Corporation" owner="请叫我头头哥"> 
    //     Copyright (C) 2015-2016 All Rights Reserved 
    //     原博文地址: http://www.cnblogs.com/toutou/ 
    // </copyright>  
    //------------------------------------------------------------------------------ 
    namespace CNBlogs.EmulationSystem 
    
        using System; 
        using System.Collections.Generic; 
        using System.Threading; 
        using System.Threading.Tasks; 
        using CNBlogs.Common.Shared; 
        class Runner 
        { 
            /// <summary> 
                    /// Job helper 
            /// </summary> 
                    private static JobHelper jobHelper = new JobHelper(); 
            /// <summary> 
                    /// The locker used to lock the semaphore and thread. 
            /// </summary> 
                    private static object lockerObj = new object(); 
            /// <summary> 
                    /// The semaphore limit the thread number of get latest test info 
            /// </summary> 
                    private static Semaphore semaphoreLimit; 
            /// <summary> 
                    /// Runner lock 
            /// </summary> 
                    private static Mutex mutex; 
            static void Main(string[] args) 
            { 
                // 记录开始处理的时间 
                DateTime paddingTime = DateTime.Now.ToLocalTime(); 
                int orderCount = jobHelper.GetJobCount(); 
                // 用一个指示调用线程是否应拥有互斥体的初始所属权的布尔值来初始化 Mutex 类的新实例。 
                // 当前进程只能启动一次 
                mutex = new Mutex(false"CNBlogs.EmulationSystem"); 
                if (!mutex.WaitOne(0false)) 
                { 
                    Console.Out.WriteLine("Monitor already running..."); 
                    return
                } 
                // 假设我们的服务器一个进程只能接受的大小=当前线程大小*500 ps:500是设置信号量的最大值 
                    int maxProcNumber = 500
                using (semaphoreLimit = new Semaphore(0, maxProcNumber)) 
                { 
                    // 以指定的次数退出信号量并返回前一个计数。 
                    semaphoreLimit.Release(maxProcNumber); 
                    var taskList = new List<Task>(); 
                    for (int i = 0; i < orderCount; i++) 
                    { 
                        int temp=i; 
                        // 如果SubmitJob有IO或者其他容易因为冲突而引起异常的话,这里需要加上lock 
                        //lock (lockerObj) 
                        //{ 
                            semaphoreLimit.WaitOne(); 
                            taskList.Add(Task.Factory.StartNew(() => 
                            { 
                                // 假设i就是job id 
                                jobHelper.SubmitJob(temp); 
                                // 退出信号量并返回前一个计数。 
                                semaphoreLimit.Release(); 
                            })); 
                        //} 
                    } 
                    // 等待所有task执行完毕 
                    Task.WaitAll(taskList.ToArray()); 
                } 
                // 记录处理完成的时间 
                DateTime completeTime = DateTime.Now.ToLocalTime(); 
                var minutes = (completeTime - paddingTime).TotalSeconds; 
                Console.WriteLine(string.Format("Complete: {0},cost {1} s", orderCount, minutes)); 
            } 
        } 
    
    
    • 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.
    • 32.
    • 33.
    • 34.
    • 35.
    • 36.
    • 37.
    • 38.
    • 39.
    • 40.
    • 41.
    • 42.
    • 43.
    • 44.
    • 45.
    • 46.
    • 47.
    • 48.
    • 49.
    • 50.
    • 51.
    • 52.
    • 53.
    • 54.
    • 55.
    • 56.
    • 57.
    • 58.
    • 59.
    • 60.
    • 61.
    • 62.
    • 63.
    • 64.
    • 65.
    • 66.
    • 67.
    • 68.
    • 69.
    • 70.
    • 71.
    • 72.
    • 73.
    • 74.
    • 75.
    • 76.
    • 77.

    代码效果:

v博客总结

多线程路还很长...

 

责任编辑:王雪燕 来源: 博客园
相关推荐

2024-06-07 00:09:50

2022-05-13 09:47:28

Docker容器

2022-09-02 12:13:22

TCPUDP场景

2020-11-24 10:13:20

测试开发管理

2025-01-03 09:39:04

2009-08-12 18:04:44

编写C#多线程

2021-04-12 06:08:16

HiveSpark大数据

2020-10-07 22:21:13

程序员技术线程

2009-09-04 15:09:48

C#多线程启动Squa

2009-08-28 16:43:57

C#多线程学习

2021-05-10 23:28:29

vRANC-RANO-RAN

2019-05-15 15:10:12

Tomcat Session Cookie

2024-08-07 08:22:27

2011-04-25 14:42:10

C#lock

2009-08-26 18:13:55

C#多线程lock

2009-08-17 16:56:51

C#多线程控制进度条

2024-10-21 16:59:37

C#编程多线程

2009-07-17 10:37:05

C#多线程

2013-02-20 10:00:16

微软CodePlexGitHub

2020-04-09 15:26:55

间谍软件NSOFacebook
点赞
收藏

51CTO技术栈公众号