Csharp/C#教程:浅谈C#多线程简单例子讲解分享

.NET将关于多线程的功能定义在System.Threading免费精选名字大全空间中。因此,要使用多线程,必须先声明引用此免费精选名字大全空间(usingSystem.Threading;)。

a.启动线程

顾名思义,“启动线程”就是新建并启动一个线程的意思,如下代码可实现:

Threadthread1=newThread(newThreadStart(Count));

其中的Count是将要被新线程执行的函数。

b.杀死线程

“杀死线程”就是将一线程斩草除根,为了不白费力气,在杀死一个线程前最好先判断它是否还活着(通过IsAlive属性),然后就可以调用Abort方法来杀死此线程。

c.暂停线程

它的意思就是让一个正在运行的线程休眠一段时间。如thread.Sleep(1000);就是让线程休眠1秒钟。

d.优先级

这个用不着解释了。Thread类中hreadPRiority属性,它用来设置优先级,但不能保证操作系统会接受该优先级。一个线程的优先级可分为5种:Normal,AboveNormal,BelowNormal,Highest,Lowest。具体实现例子如下:

thread.Priority=ThreadPriority.Highest;

e.挂起线程

Thread类的Suspend方法用来挂起线程,直到调用Resume,此线程才可以继续执行。如果线程已经挂起,那就不会起作用。

if(thread.ThreadState=ThreadState.Running) { thread.Suspend(); }

f.恢复线程

用来恢复已经挂起的线程,以让它继续执行,如果线程没挂起,也不会起作用。

if(thread.ThreadState=ThreadState.Suspended) { thread.Resume(); }

下面将列出一个例子,以说明简单的线程处理功能。此例子来自于帮助文档。

usingSystem; usingSystem.Threading; //Simplethreadingscenario:Startastaticmethodrunning //onasecondthread. publicclassThreadExample{ //TheThreadProcmethodiscalledwhenthethreadstarts. //Itloopstentimes,writingtotheconsoleandyielding //therestofitstimesliceeachtime,andthenends. publicstaticvoidThreadProc(){ for(inti=0;i<10;i++){ Console.WriteLine("ThreadProc:{0}",i); //Yieldtherestofthetimeslice. Thread.Sleep(0); } } publicstaticvoidMain(){ Console.WriteLine("Mainthread:Startasecondthread."); //TheconstructorfortheThreadclassrequiresaThreadStart //delegatethatrepresentsthemethodtobeexecutedonthe //thread.C#simplifiesthecreationofthisdelegate. Threadt=newThread(newThreadStart(ThreadProc)); //StartThreadProc.Onauniprocessor,thethreaddoesnotget //anyprocessortimeuntilthemainthreadyields.Uncomment //theThread.Sleepthatfollowst.Start()toseethedifference. t.Start(); //Thread.Sleep(0); for(inti=0;i<4;i++){ Console.WriteLine("Mainthread:Dosomework."); Thread.Sleep(0); } Console.WriteLine("Mainthread:CallJoin(),towaituntilThreadProcends."); t.Join(); Console.WriteLine("Mainthread:ThreadProc.Joinhasreturned.PressEntertoendprogram."); Console.ReadLine(); } }

此代码产生的输出类似如下内容:

Mainthread:Startasecondthread. Mainthread:Dosomework. ThreadProc:0 Mainthread:Dosomework. ThreadProc:1 Mainthread:Dosomework. ThreadProc:2 Mainthread:Dosomework. ThreadProc:3 Mainthread:CallJoin(),towaituntilThreadProcends. ThreadProc:4 ThreadProc:5 ThreadProc:6 ThreadProc:7 ThreadProc:8 ThreadProc:9 Mainthread:ThreadProc.Joinhasreturned.PressEntertoendprogram.

在VisulC#中System.Threading命名空间提供一些使得可以进行多线程编程的类和接口,其中线程的创建有以下三种方法:Thread、ThreadPool、Timer。下面就它们的使用方法逐个作一简单介绍。

一、Thread

这也许是最复杂的方法,但它提供了对线程的各种灵活控制。首先你必须使用它的构造函数创建一个线程实例,它的参数比较简单,只有一个ThreadStart委托:publicThread(ThreadStartstart);然后调用Start()启动它,当然你可以利用它的Priority属性来设置或获得它的运行优先级(enumThreadPriority:Normal、Lowest、Highest、BelowNormal、AboveNormal)。

下例首先生成了两个线程实例t1和t2,然后分别设置它们的优先级,接着启动两线程(两线程基本一样,只不过它们输出不一样,t1为“1”,t2为“2”,根据它们各自输出字符个数比可大致看出它们占用CPU时间之比,这也反映出了它们各自的优先级)。

staticvoidMain(string[]args) { Threadt1=newThread(newThreadStart(Thread1)); Threadt2=newThread(newThreadStart(Thread2)); t1.Priority=ThreadPriority.BelowNormal; t2.Priority=ThreadPriority.Lowest; t1.Start(); t2.Start(); } publicstaticvoidThread1() { for(inti=1;i<1000;i++) {//每运行一个循环就写一个“1” dosth(); Console.Write("1"); } } publicstaticvoidThread2() { for(inti=0;i<1000;i++) {//每运行一个循环就写一个“2” dosth(); Console.Write("2"); } } publicstaticvoiddosth() {//用来模拟复杂运算 for(intj=0;j<10000000;j++) { inta=15; a=a*a*a*a; } }

上述就是C#学习教程:浅谈C#多线程简单例子讲解分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/907596.html

(0)
上一篇 2021年10月24日
下一篇 2021年10月24日

精彩推荐