• C#学习系列相关之多线程(一)----常用多线程方法总结


    一、多线程的用途

            在介绍多线程的方法之前首先应当知道什么是多线程, 在一个进程内部可以执行多个任务,而这每一个任务我们就可以看成是一个线程。是程序使用CPU的基本单位。进程是拥有资源的基本单位, 线程是CPU调度的基本单位。多线程的作用不是提高执行速度,而是为了提高应用程序的使用率。我们程序在运行的使用,都是在抢CPU的时间片(执行权),如果是多线程的程序,那么在抢到
    CPU的执行权的概率应该比较单线程程序抢到的概率要大.那么也就是说,CPU在多线程程序中执行的时间要比单线程多,所以就提高了程序的使用率.但是即使是多线程程序,那么他们中的哪个线程能抢占到CPU的资源呢,这个是不确定的,所以多线程具有随机。
           多线程就好比在等待水开的同时看报纸,而不是等水开了再开始看报纸。多线程是为了同步完成多项任务,而是为了提高资源使用效率来提高系统的效率。(这个例子并不是很恰当,可以简单理解为水开和看报纸交替执行,交替的速度极快,进而可以看作是两个任务同时执行的)。

    二、常用多线程的方法

    1、Thread类

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading;
    6. using System.Threading.Tasks;
    7. namespace 线程test1005
    8. {
    9. class Program
    10. {
    11. static void Main(string[] args)
    12. {
    13. for (int i = 0; i < 300; i++)
    14. {
    15. Console.Write(1);
    16. }
    17. Thread t1 = new Thread(() =>
    18. {
    19. for (int i = 0; i < 300; i++)
    20. {
    21. Console.Write(2);
    22. } });
    23. t1.Start();
    24. for (int i = 0; i < 300; i++)
    25. {
    26. Console.Write(3);
    27. }
    28. Console.Read();
    29. }
    30. }
    31. }

       运行结果:

    2、通过Task类(最常用的方法)

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading;
    6. using System.Threading.Tasks;
    7. namespace 线程test1005
    8. {
    9. class Program
    10. {
    11. static void Main(string[] args)
    12. {
    13. for (int i = 0; i < 300; i++)
    14. {
    15. Console.Write(1);
    16. }
    17. Task t1 = new Task(() =>
    18. {
    19. for (int i = 0; i < 300; i++)
    20. {
    21. Console.Write(2);
    22. } });
    23. t1.Start();
    24. for (int i = 0; i < 300; i++)
    25. {
    26. Console.Write(3);
    27. }
    28. Console.Read();
    29. }
    30. }
    31. }

    运行结果:

    在C#中多线程的方法主要就是Task方法,效率高,速度快。

    提示:本人准备建立一个技术交流群,会将日常学习工作中遇到的问题和解决方案进行分享,同时也会将代码和学习资料上传进去,有什么不懂的问题可以咨询我!+v:SJS66-12

    生活所迫打个广告,本人也代购莆田鞋,不是中间商,工厂直接取货,价格优惠质量保证,都是我自己前去挑选,可以视频选购验货!!希望大家支持!!!赚点生活费!!!+v:SJS66-12

     3、线程池ThreadPool类

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading;
    6. using System.Threading.Tasks;
    7. namespace 线程test1005
    8. {
    9. class Program
    10. {
    11. static void Main(string[] args)
    12. {
    13. ThreadPool.QueueUserWorkItem(new WaitCallback(TestThreadPool), new string[] { "test" });
    14. for (int i = 0; i < 300; i++)
    15. {
    16. Console.Write(2);
    17. }
    18. Console.ReadKey();
    19. }
    20. public static void TestThreadPool(object state)
    21. {
    22. string[] arry = state as string[];//传过来的参数值
    23. int workerThreads = 0;
    24. int CompletionPortThreads = 0;
    25. for (int i = 0; i < 300; i++)
    26. {
    27. Console.Write(1);
    28. }
    29. ThreadPool.GetMaxThreads(out workerThreads, out CompletionPortThreads);
    30. Console.WriteLine(DateTime.Now.ToString() + "---" + arry[0] + "--workerThreads=" + workerThreads + "--CompletionPortThreads" + CompletionPortThreads);
    31. }
    32. }
    33. }

    运行结果:

    4、通过begininvoke方法

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading;
    6. using System.Threading.Tasks;
    7. namespace 线程test1005
    8. {
    9. class Program
    10. {
    11. static void Main(string[] args)
    12. {
    13. Action a = teat;
    14. a.BeginInvoke(null, null);
    15. for (int i = 0; i < 300; i++)
    16. {
    17. Console.Write(2);
    18. }
    19. Console.ReadKey();
    20. }
    21. static void teat()
    22. {
    23. for (int i = 0; i < 300; i++)
    24. {
    25. Console.Write(1);
    26. }
    27. }
    28. }
    29. }

    运行结果:

    5、async和await方法

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading;
    6. using System.Threading.Tasks;
    7. namespace 线程test1005
    8. {
    9. class Program
    10. {
    11. static void Main(string[] args)
    12. {
    13. test();
    14. for (int i = 0; i < 300; i++)
    15. {
    16. Console.Write(3);
    17. }
    18. Console.Read();
    19. }
    20. public static async void test()
    21. {
    22. for (int i = 0; i < 300; i++)
    23. {
    24. Console.Write(1);
    25. }
    26. await Task.Run(()=> {
    27. for (int i = 0; i < 300; i++)
    28. {
    29. Console.Write(2);
    30. }
    31. });
    32. }
    33. }
    34. }

    运行结果:

    本文介绍这几种C#中开启多线程的方法,在后续学习中,会对每一种线程方法进行更深一步的介绍,希望大家多多关注。

  • 相关阅读:
    【linux】日常用的linux命令总结
    Protobuf编码规则
    第一个Shader Graph
    美团组件化事件总线方案改进:ModularEventBus
    在 msys2/mingw 下安装及编译 opencv
    测试员入职新公司如何快速熟悉新业务?
    《算法通关村第一关——链表青铜挑战笔记》
    vue中的.sync修饰符
    YBT 1.1 贪心算法
    云服务器相比较物理服务器的好处有哪些?
  • 原文地址:https://blog.csdn.net/qiaodahua/article/details/133582019