当前位置: 移动技术网 > IT编程>开发语言>c# > C#线程处理 :一、线程基础

C#线程处理 :一、线程基础

2020年04月27日  | 移动技术网IT编程  | 我要评论
本笔记摘抄自:https://www.cnblogs.com/zhili/archive/2012/07/18/Thread.html,记录一下学习,方便后面资料查找 一、线程的介绍 进程(Process)是应用程序的实例要使用的资源的一个集合,每个应用程序都在各自的进程中运行来确保应用程序不受其他 ...

本笔记摘抄自:https://www.cnblogs.com/zhili/archive/2012/07/18/thread.html,记录一下学习,方便后面资料查找

    一、线程的介绍

    进程(process)是应用程序的实例要使用的资源的一个集合,每个应用程序都在各自的进程中运行来确保应用程序不受其他应用程序的影响。

    线程是进程中基本执行单元, 一个进程中可以包含多个线程。在进程入口执行的第一个线程是一个进程的主线程,在.net应用程序中,都是以main()方法

作为程序的入口(线程是进程的执行单元,进程是线程的一个容器)。

    二、线程调度和优先级  

    windows之所以被称为抢占式多线程操作系统,是因为线程可以在任意时间被抢占,并调度另一个线程。

    每个线程都分配了从0~31的一个优先级,系统首先把高优先级的线程分配给cpu执行。

    windows 支持7个相对线程优先级:idle、lowest、below normal、normal、above normal、highest和time-critical。normal是默认的线程优先级,

然而在程序中可以通过设置thread的priority属性来改变线程的优先级,它的类型为threadpriority枚举类型:lowest、belownormal、normal、abovenormal

和highest,clr为自己保留了 idle和time-critical优先级。

    枚举值列表如下:

成员名称 说明
lowest 可以将thread置于其他优先级线程之后。
belownormal 可以将thread置于normal优先级线程之后lowest优先级线程之前。
normal

可以将thread置于abovenormal优先级线程之后belownormal优先级线程之前。

默认情况下,线程置于normal优先级。

abovenormal 可以将thread置于highest优先级线程之后normal优先级线程之前。
highest 可以将thread置于其他优先级线程之前。

    三、前台线程和后台线程

    在.net中线程分为前台线程和后台线程:

    1、主线程是程序开始时就执行的,如果你需要再创建线程,那么创建的线程就是这个主线程的子线程,它是前台线程。 

    2、子线程可以是前台线程也可以是后台线程。

    3、前台线程必须全部执行完,即使主线程关闭掉,这时进程仍然存活。

    4、当所有前台线程停止运行时,clr会强制结束仍在运行的任何后台线程,这些后台线程直接被终止,不会抛出异常。

    5、前台线程与后台线程唯一的区别是后台线程不会阻止进程终止,可以在任何时候将前台线程修改为后台线程。

复制代码
        static void main(string[] args)
        {
            threadtype();
        }

        /// <summary>
        /// 前台线程与后台线程
        /// </summary>
        private static void threadtype()
        {
            //创建一个新线程(默认为前台线程)
            thread backthread = new thread(worker)
            {
                //将线程更改为后台线程
                isbackground = true
            };

            //通过start方法启动线程
            backthread.start();

            //如果backthread是前台线程,则应用程序5秒后才终止。
            //如果backthread是后台线程,则应用程序立即终止。
            console.writeline("it's from main thread.");
            //console.read();
        }

        private static void worker()
        {
            //休息5秒
            thread.sleep(5000);
            console.writeline("it's from worker thread.");
        }
复制代码

    假如保留isbackground = true;但又想继续执行worker()方法的话,可以调用thread.join()方法来实现。join()方法能保证主线程(前台线程)在异步线程

thread(后台线程)运行结束后才会运行。

    注1:一个线程在执行的过程中,可能调用另一个线程,前者可以称为调用线程,后者成为被调用线程。

    注2:thread.join()方法的使用场景:调用线程挂起,等待被调用线程执行完毕后,继续执行。

    注3:被调用线程执行join方法,告诉调用线程,你先暂停,我执行完了,你再执行。从而保证了先后关系。

复制代码
        static void main(string[] args)
        {
            threadstatuschange();
        }

        /// <summary>
        /// 线程状态之间的转换
        /// </summary>
        private static void threadstatuschange()
        {
            //创建一个新线程(默认为前台线程)
            thread backthread = new thread(worker)
            {
                //将线程更改为后台线程
                isbackground = true
            };

            //通过start方法启动线程
            backthread.start();

            //join()方法能保证主线程(前台线程)在异步线程thread(后台线程)运行结束后才会运行
            backthread.join();

            console.writeline("it's from main thread.");
            console.read();
        }

        private static void worker()
        {
            //休息5秒
            thread.sleep(5000);
            console.writeline("it's from worker thread.");
        }
复制代码

    运行结果如下:

    四、 suspend和resume方法

    这两个方法在.net framework 1.0的时候就支持的方法,他们分别可以挂起线程及恢复挂起的线程,但在.net framework 2.0以后的版本中这两个方法都过时了。

    msdn的解释是这样:

    警告:

    不要使用suspend和resume方法来同步线程的活动。您无法知道挂起线程时它正在执行什么代码。如果您在安全权限评估期间挂起持有锁的线程,

则appdomain中的其他线程可能被阻止。如果您在线程正在执行类构造函数时挂起它,则 appdomain中尝试使用该类的其他线程将被阻止。这样很容易发生死锁。

复制代码
        static void main(string[] args)
        {
            threadresume();
        }

        /// <summary>
        /// 线程恢复
        /// </summary>
        private static void threadresume()
        {
            thread thread = new thread(threadsuspend)
            {
                name = "thread1"
            };
            thread.start();
            thread.sleep(2000);
            console.writeline("main thread is running.");
            //线程恢复
            thread.resume();
            console.read();
        }

        /// <summary>
        /// 线程挂起
        /// </summary>
        private static void threadsuspend()
        {
            console.writeline("thread: {0} has been suspended.", thread.currentthread.name);
            //将当前线程挂起
            thread.currentthread.suspend();
            console.writeline("thread: {0} has been resumed.", thread.currentthread.name);
        }
复制代码

    在上面这段代码中thread1线程是在主线程中恢复的,但当主线程发生异常时,这时候thread1就会一直处于挂起状态,此时thread1所使用的资源就不能释放

(除非强制终止进程),当其它的线程需要使用这快资源的时候, 很有可能就会发生死锁现象。

    上面一段代码还存在一个隐患,假如把thread.sleep(2000);这段代码注释一下:

复制代码
        static void main(string[] args)
        {
            threadresume();
        }

        /// <summary>
        /// 线程恢复
        /// </summary>
        private static void threadresume()
        {
            thread thread = new thread(threadsuspend)
            {
                name = "thread1"
            };
            thread.start();
            //thread.sleep(2000);
            console.writeline("main thread is running.");
            //线程恢复
            thread.resume();
            console.read();
        }

        /// <summary>
        /// 线程挂起
        /// </summary>
        private static void threadsuspend()
        {
            console.writeline("thread: {0} has been suspended.", thread.currentthread.name);
            //将当前线程挂起
            thread.currentthread.suspend();
            console.writeline("thread: {0} has been resumed.", thread.currentthread.name);
        }
复制代码

    这个时候,主线程因为跑(运行)得太快,做完自己的事情去唤醒thread1时,此时thread1还没有挂起,而此时唤醒thread1就会出现异常了。

    五、abort和interrupt方法

    abort方法和interrupt都是用来终止线程的,但是两者还是有区别的:

    1、它们抛出的异常不一样:abort 方法抛出的异常是threadabortexception,interrupt抛出的异常为threadinterruptedexception。

    2、调用interrupt方法的线程之后可以被唤醒,然而调用abort方法的线程就直接被终止不能被唤醒了。

    下面演示abort方法的使用:

复制代码
        static void main(string[] args)
        {
            //threadtype();
            //threadstatuschange();
            //threadresume();
            threadabort();
        }        
        
        /// <summary>
        /// 线程中断(不可再唤醒)
        /// </summary>
        private static void threadabort()
        {
            thread threadabort = new thread(abortmethod)
            {
                name = "threadabort"
            };
            threadabort.start();
            thread.sleep(1000);
            try
            {
                threadabort.abort();
            }
            catch
            {
                console.writeline("1-> {0} exception happen in main thread.", thread.currentthread.name);
                console.writeline("2-> {0} status is:{1} in main thread.", thread.currentthread.name, thread.currentthread.threadstate);
            }
            finally
            {
                console.writeline("3-> {0} status is:{1} in main thread.", threadabort.name, threadabort.threadstate);
            }
            threadabort.join();
            console.writeline("4-> {0} status is:{1}", threadabort.name, threadabort.threadstate);
            console.read();
        }

        /// <summary>
        /// abort方法
        /// </summary>
        private static void abortmethod()
        {
            try
            {
                thread.sleep(5000);
            }
            catch (exception e)
            {
                console.writeline(e.gettype().name);
                console.writeline("5-> {0} exception happen in abort thread.", thread.currentthread.name);
                console.writeline("6-> {0} status is:{1} in abort thread.", thread.currentthread.name, thread.currentthread.threadstate);
            }
            finally
            {
                console.writeline("7-> {0} status is:{1} in abort thread.", thread.currentthread.name, thread.currentthread.threadstate);
            }
        }
复制代码

    运行结果如下:

    从运行结果可以看出,调用abort方法的线程引发的异常类型为threadabortexception,另外异常只会在调用abort方法的线程中发生,而不会在主线程中抛出,

其次调用abort方法后线程的状态不是立即改变为aborted状态,而是从abortrequested->aborted。 

    下面演示interrupt方法的使用:

复制代码
        static void main(string[] args)
        {
            threadinterrupt();
        }
        
        /// <summary>
        /// 线程中断(可再唤醒)
        /// </summary>
        static void threadinterrupt()
        {
            thread threadinterrupt = new thread(interruptmethod)
            {
                name = "threadinterrupt"
            };
            threadinterrupt.start();
            threadinterrupt.interrupt();
            threadinterrupt.join();
            console.writeline("1-> {0} status is:{1} ", threadinterrupt.name, threadinterrupt.threadstate);
            console.read();
        }

     
        /// <summary>
        /// interrupt方法
        /// </summary>
        private static void interruptmethod()
        {
            try
            {
                thread.sleep(5000);
            }
            catch (exception e)
            {
                console.writeline(e.gettype().name);
                console.writeline("2-> {0} exception happen in interrupt thread.", thread.currentthread.name);
                console.writeline("3-> {0} status is:{1} in interrupt thread.", thread.currentthread.name, thread.currentthread.threadstate);
            }
            finally
            {
                console.writeline("4-> {0} status is:{1} in interrupt thread.", thread.currentthread.name, thread.currentthread.threadstate);
            }
        }
复制代码

    运行结果如下:

    从结果中可以得到,调用interrupt方法抛出的异常为:threadinterruptexception, 另外当调用interrupt方法后线程的状态应该是中断的,但是从运行结果看,

此时的线程因为join、sleep方法而唤醒了线程。

    为了进一步解释调用interrupt方法的线程可以被唤醒, 我们可以在线程执行的方法中运用循环,如果线程可以唤醒,则输出结果中就一定会有循环的部分,

而调用abort方法的线程则不会有循环的部分。

    下面代码相信大家看后肯定会更加理解两个方法的区别:

复制代码
        static void main(string[] args)
        {
            //threadtype();
            //threadstatuschange();
            //threadresume();
            //threadabort();
            //threadinterrupt();
            threadwake();
        }

        /// <summary>
        /// 线程唤醒
        /// </summary>
        static void threadwake()
        {
            thread threadwake = new thread(wakemethod);
            threadwake.start();
            thread.sleep(100);

            threadwake.interrupt();
            thread.sleep(3000);
            console.writeline("1-> after finally block,the threadwake status is:{0}", threadwake.threadstate);
            console.read();
        }

        /// <summary>
        /// wake方法
        /// </summary>
        private static void wakemethod()
        {
            for (int i = 0; i < 4; i++)
            {
                try
                {
                    thread.sleep(2000);
                    console.writeline("2-> thread is running.");
                }
                catch (exception ex)
                {
                    if (ex != null)
                    {
                        console.writeline("3-> exception {0} throw.", ex.gettype().name);
                    }
                }
                finally
                {
                    console.writeline("4-> current thread status is:{0}", thread.currentthread.threadstate);
                }
            }
        }
复制代码

    运行结果如下:

    如果把上面的threadwake.interrupt();改为threadwake.abort(); 运行结果为:

    六、简单线程的使用

    其实在上面介绍前台线程和后台线程的时候已经通过threadstart委托创建一个线程了,此时已经实现了一个多线程的一个过程。

    下面通过parameterizedthreadstart委托的方式来实现多线程:

复制代码
        static void main(string[] args)
        {
            threadtypeuseparameterized();
        }

        /// <summary>
        /// 前台线程与后台线程(使用parameterizedthreadstart委托的方式来实现多线程)
        /// </summary>
        private static void threadtypeuseparameterized()
        {
            //创建一个新线程(默认为前台线程)
            thread backthread = new thread(new parameterizedthreadstart(worker1));

            //通过start方法启动线程
            backthread.start(123);

            //如果backthread是前台线程,则应用程序5秒后才终止。
            //如果backthread是后台线程,则应用程序立即终止。
            console.writeline("it's from main thread.");
        }

        private static void worker1(object parameter)
        {
            //休息5秒
            thread.sleep(5000);
            console.writeline(parameter+" is from worker1 thread.");
            console.read();
        }
复制代码

    运行结果如下:

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网