背景:
在.NET Framework1.0时代就诞生了Thread,也有大量相关的Api支持,thread创建是前台线程
缺陷:
1、一次使用一个线程来创建删除线程,线程的创建和销毁对系统开销比较大
2、在高并发的场景,大量线程创建、销毁会降低响应性能
 
一、构造方法
1.1带参数的委托的构造方法——Thread(ParameterizedThreadStart)
ParameterizedThreadStart构造方法,定义:public delegate void ParameterizedThreadStart(object? obj)带参数的Action委托
public static void Dosomething(object? word)
        {
            Console.WriteLine(word.ToString());
        }
                ParameterizedThreadStart ps = new ParameterizedThreadStart(Dosomething);
                Thread thread1 = new Thread(ps);
                thread1.Start("埃里克多");

1.2不带参数的构造方法——Thread(ThreadStart)

ThreadStart构造方法,定义:public delegate void ThreadStart();

ThreadStart ts = new ThreadStart(Dothing);
                Thread thread2 = new Thread(ts);
                thread2.Start();
       public static void Dothing()
        {
            Console.WriteLine("这是个无参数的方法。。。。");
        }

 

二、相关属性及方法

        Thread.Start():启动线程的执行;

  Thread.Suspend():挂起线程,或者如果线程已挂起,则不起作用;

  Thread.Resume():继续已挂起的线程;

  Thread.Interrupt():中止处于 Wait或者Sleep或者Join 线程状态的线程;

  Thread.Join():阻塞调用线程,直到某个线程终止时为止

  Thread.Sleep():将当前线程阻塞指定的毫秒数,此时线程处于休眠状态,期间此线程不再得到系统分配的执行时间片。

  Thread.Abort():以开始终止此线程的过程。

2.1线程的执行是无序的

                  DisorderVerify();
                //多次执行后会得到下面两种结果
                //结果一:
                //Thread_2 is start
                //Thread_1 is start
                //结果二:
                //Thread_1 is start
                //Thread_2 is start
        static void DisorderVerify()
        {
            Thread thread1 = new Thread(() => Console.WriteLine("Thread_1 is start"));
            thread1.Name = "Thread_1";


            Thread thread2 = new Thread(() => Console.WriteLine("Thread_2 is start"));
            thread2.Name = "Thread_2";

            thread1.Start();
            thread2.Start();
        }

2.2 Thread.join函数

当thread1调用Join方法的时候,主线程MainThread、thread2就被停止执行,直到thread1线程执行完毕,MainThread、thread2存在交叉执行

                for (int i = 0; i < 20; i++)
                {
                    if (i == 4)
                    {
                        JoinMethodTest();
                    }
                    else
                    {
                        Console.WriteLine($"主线程执行:{i}");
                    }
                    
                }
        static void JoinMethodTest()
        {
            Thread thread1 = new Thread(() =>
            {
                for (int i = 0; i <10; i++)
                {
                    Console.WriteLine("ddd");
                }
                
            });
            Thread thread2 = new Thread(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("aaa");
                }

            });
            thread1.Start();
            thread1.Join();
            //等待thread1执行完再执行
            thread2.Start();
        }

执行结果:

                //主线程执行:1
                //主线程执行:2
                //主线程执行:3
                //ddd
                //ddd
                //ddd
                //ddd
                //ddd
                //ddd
                //ddd
                //ddd
                //ddd
                //ddd
                //主线程执行:5
                //主线程执行:6
                //主线程执行:7
                //主线程执行:8
                //主线程执行:9
                //主线程执行:10
                //主线程执行:11
                //主线程执行:12
                //主线程执行:13
                //aaa
                //aaa
                //aaa
                //aaa
                //aaa
                //aaa
                //aaa
                //aaa
                //aaa
                //aaa
                //主线程执行:14
                //主线程执行:15
                //主线程执行:16
                //主线程执行:17
                //主线程执行:18
                //主线程执行:19

 

三、前台线程及后台线程

Thread默认创建的为前台线程,可以看到当thread 为前台线程时,即使主线程执行结束,thread也会执行后程序才会结束;当threa为后台线程时,主线程执行结束,程序就结束了。

        public static void BackGroundThreadTest()
        {
            Thread thread = new Thread(() =>
            {
                Thread.Sleep(3000);
                Console.WriteLine("后台线程测试");
            });
            thread.IsBackground = true;
            thread.Start();

        }
                //后台方法测试
                BackGroundThreadTest();
                Console.WriteLine("主线程结束。。。。");
                //打印结果:
                //主线程结束。。。。

                //将方法里的thread.IsBackground = true;注释
                //打印结果:
                //主线程结束。。。。
                //后台线程测试