• C#多线程详解(三)


    继续上一节的问题:调换两个新创建的线程启动顺序会是什么结果?

    using System;

    using System.Threading;

    namespace Test
    {
        class TestThread
        {
            private static void FirstThreadFun()
            {
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine(Thread.CurrentThread.Name + "   i =  " + i);
                }
                Console.WriteLine(Thread.CurrentThread.Name + "的优先级:" + Thread.CurrentThread.Priority);
                Console.WriteLine(Thread.CurrentThread.Name + " 执行完毕");
            }

            private static void SecondThreadFun()
            {
                for (int j = 0; j < 10; j++)
                {
                    Console.WriteLine(Thread.CurrentThread.Name + "   j =  " + j);
                }
                Console.WriteLine(Thread.CurrentThread.Name + "的优先级:" + Thread.CurrentThread.Priority);
                Console.WriteLine(Thread.CurrentThread.Name + " 执行完毕");
            }

            static void Main(string[] args)
            {
                //程序调用了Main()函数时,就起动了一个进程,同时也启动了一个线程[这个线程就是主线程].
                Thread.CurrentThread.Name = "MainThread";
                            
                Thread secondThread = new Thread(new ThreadStart(TestThread.SecondThreadFun));
                //取名为 secondThread
                secondThread.Name = "secondThread";
                //设定线程的优先级为AboveNormal[在所有创建的新线程中,执行顺序为第二]
                secondThread.Priority = ThreadPriority.AboveNormal;

                //创建第一个线程
                Thread firstThread = new Thread(new ThreadStart(TestThread.FirstThreadFun));
                //取名为 firstThread
                firstThread.Name = "firstThread";
                //设定线程的优先级为最高级[在所有创建的新线程中,最先执行.线程优先级默认为Normal,共5 级:Highest,AboveNormal,Normal,BelowNormal,Lowest]
                firstThread.Priority = ThreadPriority.Highest;

                for (int z = 0; z < 20; z++)
                {
                    if (z == 10)

                    {

             //这里与上一节的代码略有不同

                        secondThread.Start();

                        firstThread.Start();
                        firstThread.Join();

                        secondThread.Join();
                    }
                    else
                    {
                        Console.WriteLine(Thread.CurrentThread.Name + "   z =  " + z);
                    }
                }
                Console.Read();
            }
        }

    }

    运行结果:

    图1                                                  图2                                                 图3

     

    总结:

    1.上面三个图的运行结果都不一样,但有一点是相同的.主线程是最先执行的[不管新创建的线程级别如何]

    2.当满足新线程启动条件后,secondThread总是先执行,是因为secondThread线程先启动了[尽管firstThread优先级高于secondThread] .

    3.当secondThread运行了一小段时间后,firstThread线程接着执行.

    4.当两个新创建的线程都执行完毕后,主线程继续执行.

    注:

    1.多线程同时工作的时候,优先级高的线程要先启动[Thread.Start()],先加入[Thread.Join()].

    2.这个例子里因为启动顺序搞错了,也可以看出多线程,并不是说所有的线程同时工作,而是轮流运行的.

    Thread.ThreadState 属性

    这个属性代表了线程运行时状态,在不同的情况下有不同的值,我们有时候可以通过对该值的判断来设计程序流程。
    ThreadState 属性的取值如下:
    Aborted:线程已停止;
    AbortRequested:线程的Thread.Abort()方法已被调用,但是线程还未停止;
    Background:线程在后台执行,与属性Thread.IsBackground有关;
    Running:线程正在正常运行;
    Stopped:线程已经被停止;
    StopRequested:线程正在被要求停止;
    Suspended:线程已经被挂起(此状态下,可以通过调用Resume()方法重新运行);
    SuspendRequested:线程正在要求被挂起,但是未来得及响应;
    Unstarted:未调用Thread.Start()开始线程的运行;
    WaitSleepJoin:线程因为调用了Wait(),Sleep()或Join()等方法处于封锁状态;

    上面提到了Background状态表示该线程在后台运 行,那么后台运行的线程有什么特别的地方呢?其实后台线程跟前台线程只有一个区别,那就是后台线程不妨碍程序的终止。一旦一个进程所有的前台线程都终止 后,CLR(通用语言运行环境)将通过调用任意一个存活中的后台进程的Abort()方法来彻底终止进程。

     

     
  • 相关阅读:
    <s:property>的用法(jsp获取action中的值或者方法)
    struts2 Action获取表单数据
    form标签中id和name属性的区别
    button和submit区别
    hibernate could not resolve property
    Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExce
    宏定义#define和内联函数inline的区别
    线程绑定cpu
    posix系统线程调度-设置线程优先级
    std::lock_guard和std::unique_lock的区别
  • 原文地址:https://www.cnblogs.com/bicabo/p/1647941.html
Copyright © 2020-2023  润新知