多线程技术应用(一)

亦凉 2022-05-12 09:12 259阅读 0赞

DYT_JCBK(1/900:2018/9/16)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadTest
{
class Program
{
static void Main(string[] args)
{

  1. \#region 1.无参的调用方法
  2. //Thread thread1 = new Thread(new ThreadStart(Thread1));//创建无参的线程
  3. //thread1.Start();//调用start方法执行线程
  4. //Console.ReadKey();
  5. \#endregion
  6. \#region 2.通过匿名委托或lambda表达式来为Thread构造方法赋值
  7. //Thread thread1 = new Thread(delegate () \{ Console.WriteLine("匿名委托建立线程"); \});
  8. //thread1.Start();
  9. //Thread thread2 = new Thread(() => Console.WriteLine("我是通过Lambda表达式创建的委托"));
  10. //thread2.Start();
  11. //Console.ReadKey();
  12. \#endregion
  13. \#region 3.ParameterizedThreadStart是一个有参的,返回值为void的委托,委托参数的类型必须是Object
  14. //Thread thread = new Thread(new ParameterizedThreadStart(Thread1));
  15. //thread.Start("这是一个有参数的委托");
  16. //Console.ReadKey();
  17. \#endregion
  18. \#region 4.线程的常用方法
  19. /\*
  20. Abort() 终止线程;
  21. GetDomain() 返回当前线程正在其中运行的当前域
  22. GetDomainId() 返回当前线程正在其中运行的当前域ID
  23. Join() 已重载.阻塞调用线程,直到某个线程终止时为止
  24. Interrupt() 中断处于WaitSleepJoin终止时为止
  25. Resume() 继续运行已经挂起的线程
  26. Start() 执行本线程
  27. Suspend() 挂起当前线程,如果当前线程处于已经挂起的状态则不起作用
  28. Sleep() 把正在执行的线程挂起一段时间
  29. \*/
  30. \#endregion
  31. \#region 5.Thread的方法练习
  32. 获取正在运行的线程
  33. //Thread thread = Thread.CurrentThread;
  34. 设置线程的名字
  35. //thread.Name = "Main Thread";
  36. 获取当前线程的唯一标识符
  37. //int id = thread.ManagedThreadId;
  38. 获取当前线程的状态
  39. //ThreadState state = thread.ThreadState;
  40. //ThreadPriority priority = thread.Priority;
  41. //string msg = string.Format("Thread ID:\{0\}\\n" + "Thread Name:\{1\}\\n" + "Thread State:\{2\}\\n" + "Thread Priority:\{3\}\\n", id, thread.Name, state, priority);
  42. //Console.WriteLine(msg);
  43. //Console.ReadKey();
  44. \#endregion
  45. \#region 6.前台线程和后台线程
  46. /\*\*
  47. \* 前台线程:只有所有的前台线程都结束,应用程序才能结束,默认情况下创建的线程都是前台线程
  48. \* 后台线程:只要所有的前台线程结束,后台线程自动结束,通过Thread>IsBackground设置后台线程,必须在start()方法之前设置线程类型,因为一旦运行,无法
  49. \* 改变类型
  50. \* /\*/
  51. //演示先后台线程
  52. BackGroundTest backGround = new BackGroundTest(10);
  53. //创建前台线程
  54. Thread fthread = new Thread(new ThreadStart(backGround.RunLoop));
  55. //给线程命名
  56. fthread.Name = "前台线程";
  57. BackGroundTest backGround1 = new BackGroundTest(20);
  58. //创建后台线程
  59. Thread bthread = new Thread(new ThreadStart(backGround1.RunLoop));
  60. bthread.Name = "后台线程";
  61. //设置为后台线程
  62. bthread.IsBackground = true;
  63. //启动线程
  64. fthread.Start();
  65. bthread.Start();
  66. \#endregion
  67. \}
  68. \#region 1.无参的调用方法
  69. //public static void Thread1()
  70. //\{
  71. // Console.WriteLine("这是无参的方法");
  72. //\}
  73. \#endregion
  74. \#region 3.ParameterizedThreadStart是一个有参的,返回值为void的委托
  75. //public static void Thread1(Object obj)
  76. //\{
  77. // Console.WriteLine(obj);
  78. //\}
  79. \#endregion
  80. \}
  81. \#region 6.前台线程和后台线程
  82. class BackGroundTest
  83. \{
  84. private int count;
  85. public BackGroundTest(int count)
  86. \{
  87. this.count = count;
  88. \}
  89. public void RunLoop()
  90. \{
  91. //获取当前线程的名称
  92. string threadName = Thread.CurrentThread.Name;
  93. for (int i = 0; i < count; i++)
  94. \{
  95. Console.WriteLine("\{0\}计数:\{1\}", threadName, i.ToString());
  96. //线程休眠500ms
  97. Thread.Sleep(500);
  98. \}
  99. Console.WriteLine("\{0\}完成计数", threadName);
  100. \}
  101. \}
  102. \#endregion

}

发表评论

表情:
评论列表 (有 0 条评论,259人围观)

还没有评论,来说两句吧...

相关阅读

    相关 线技术

    进程和线程都是操作系统的概念。进程是应用程序的执行实例,每个进程是由私有的虚拟地址空间、代码、数据和其它各种系统资源组成,进程在运行过程中创建的资源随着进程的终止而被销毁,所使