在项目中经常用到线程Thread,先做个简单记录,后面再完善下,方便以后参考。本人技术有限,如有不同见解之处,欢迎博友批评指正。

执行的线程Thread分无参数的,一个参数,多个参数的。直接看代码吧。

NetFramework4.0后,出现了Task(本质还是Thread),这个效率更高,分配资源更合理。

一、没有参数的

        static void Main(string[] args)
        {
            Console.WriteLine("主线程开始了...");
            Console.WriteLine("主线程Thread:"+Thread.CurrentThread.ManagedThreadId);
            Thread thread = new Thread(ThreadTestMthod);
            thread.Start();
            for (int i = 0; i < 50; i++)
            {
                Console.WriteLine("Num:"+i);
            }
            
            Console.ReadKey();
        }
        static void ThreadTestMthod()
        {
            Console.WriteLine("当前线程Thread:"+Thread.CurrentThread.ManagedThreadId);
            for (int i = 50; i < 100; i++)
            {
                Console.WriteLine("Num:"+i);
            }
        }
View Code

相关文章:

  • 2022-12-23
  • 2021-05-12
  • 2022-01-26
  • 2022-01-19
  • 2021-10-29
  • 2021-09-11
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-22
  • 2021-12-10
  • 2021-05-26
  • 2021-07-26
  • 2021-10-22
  • 2022-12-23
  • 2021-07-22
相关资源
相似解决方案