标签:
重要的Thread类在System.Threading中。System.Threading 命名空间提供类和接口,使多线程的编程。 除了用于同步线程活动和访问数据的类 (Mutex, ,Monitor, ,Interlocked, ,AutoResetEvent, ,依此类推),此命名空间包括 ThreadPool 类,它允许您使用的系统提供线程池和 Timer 在线程池线程执行的回调方法的类。
1.Thread 类
Thread类创建和控制线程,设置其优先级并获取其状态。
进程启动时,公共语言运行时将自动创建一个前台线程执行应用程序代码。 此主前台线程,以及一个进程可以创建一个或多个线程以执行与该进程关联的程序代码的一部分。 在前台或后台中,可以执行这些线程。 此外,您可以使用 ThreadPool 类在由公共语言运行时管理的工作线程上执行代码。
启动线程 通过提供一个委托,表示该线程是在其类构造函数中执行的方法来启动一个线程。 然后,可以调用 Start 方法开始执行。Thread 构造函数可以采用两个委托类型,具体取决于是否可以将参数传递给要执行的方法.
线程对象的检索问题您可以使用静态 (Shared 在 Visual Basic 中) CurrentThread 属性来检索对当前执行线程的线程正在执行的代码中的引用。
前台和后台线程实例 Thread 类表示前台线程或后台线程。 后台线程将与前台线程有一处例外 ︰ 如果所有前台线程已都终止,后台线程不会保留正运行的进程。 一旦所有前台线程已都停止,则运行时将停止所有后台线程,并关闭。
区域性和线程每个线程都有一个区域性,由表示 CurrentCulture 属性和 UI 区域性,由表示 CurrentUICulture 属性。 当前区域性支持分析和格式设置,字符串比较和排序,诸如区分区域性的操作,还可以控制书写系统和线程使用的日历。 当前 UI 区域性提供用于区分区域性检索资源文件中的资源。
实例化一个新线程时,其区域性和 UI 区域性定义的当前系统区域性和 UI 区域性,而不是区域性和 UI 区域性从中创建新线程的线程。 这意味着,例如,如果当前系统区域性为英语 (美国),主要应用程序线程的当前区域性为法语 (法国),通过调用创建一个新线程的区域性 Thread(ParameterizedThreadStart) 从主线程的构造函数是英语 (美国) 和不是法语 (法国)。
2.线程总览
Thread的方法:
Thread的属性
3.创建线程
用New Thread( )创建一个新的线程,再用Start执行。
线程ID:ID是创建线程时操作系统自动生成的,用来标记线程的(线程编号),由操作系统来管理且不得重复(即线程ID是唯一的),此ID只在线程的生存期内有效,线程消失则该ID撤消。
线程Handle:为一个32位指针。OS有一张维护HANDLE的表单,里面HANDLE的引用计数和有关的属性,尽管相同的句柄一定标识同一线程,但同一线程可能拥有两个打开的句柄,因此,不能用句柄来区分两个线程是否是同一线程。
Imports System.Threading Public Class SimpleThread Public Shared Sub main() Dim obj As New SimpleThread obj.SimpleMethod() '主线程执行 '----------- 'Dim t As New Thread(AddressOf obj.SimpleMethod) '1、ThreadStar上委托调用实例的方法 Dim ts As New ThreadStart(AddressOf obj.SimpleMethod) '2.同1先指定执行方法,再创建、执行 Dim t As New Thread(ts) '---------- t.Start() '线程执行 Console.Read() End Sub Public Sub SimpleMethod() Dim i As Integer = 5 Dim x As Integer = 10 Dim iResult As Integer = i * x Console.WriteLine("Result is " & iResult.ToString & " from thread ID:" & Thread.CurrentThread.ManagedThreadId.ToString) '显示托管线程ID End Sub End Class例:再看一下新建多个线程时,注意一下ID编号和ID出现的顺序。(运行程序时,本机开了360浏览器、word、OCR软件、优酷PC端、QQ等多个程序)
Imports System.Threading Public Class SimpleThread Public Shared Sub main() Dim i As Integer For i = 1 To 10 Dim t As New Thread(AddressOf SimpleMethod) t.Start() Next Console.Read() End Sub Public Shared Sub SimpleMethod() Console.WriteLine("Worker Thread: " & Thread.CurrentThread.ManagedThreadId.ToString) End Sub End Class
Imports System.Threading Public Class ThreadStartBranding Enum UserClass '枚举 ClassAdmin ClassUser End Enum Shared Sub main() ExecuteFor(UserClass.ClassAdmin) ExecuteFor(UserClass.ClassUser) Console.Read() End Sub Shared Sub ExecuteFor(ByVal uc As UserClass) Dim ts As ThreadStart Dim tsAdmin As New ThreadStart(AddressOf AdminMethod) '1.ThreadStart 委托 Dim tsUser As New ThreadStart(AddressOf UserMethod) If uc = UserClass.ClassUser Then '2.选择决定哪个委托方法 ts = tsUser Else ts = tsAdmin End If Dim t As New Thread(ts) '3.构造线程 t.Start() '4.执行 End Sub Shared Sub AdminMethod() Console.WriteLine("Admin Method") End Sub Shared Sub UserMethod() Console.WriteLine("User Method") End Sub End Class
Imports System.Threading Public Class ThreadState Shared Sub main() Dim t As New Thread(AddressOf WorkerFunction) t.Start() '4、是否后台线程 Console.WriteLine("Backgroud is " & Thread.CurrentThread.IsBackground & " for main thread.") Console.WriteLine("Backgroud is " & t.CurrentThread.IsBackground & " for new thread.") '------- While t.IsAlive '主线程每睡眠200毫秒后,时钟中断激活主线程(线程启动后未停止时为真) Console.WriteLine("Waiting,main thread is back to sleep.") Thread.CurrentThread.Sleep(200) '1、主线程睡眠 End While '------- Console.WriteLine("Complete,new thread state is " & t.ThreadState.ToString) '3、新线程最后状态 Console.Read() End Sub Shared Sub WorkerFunction() '模拟功能,每隔5000毫秒显示新建线程状态 For i As Integer = 1 To 50000 If i Mod 5000 = 0 Then '2、新建线程的状态(这里的当前线程指的是主线程中创建的新线程) Console.WriteLine("Worker: " & Thread.CurrentThread.ThreadState.ToString) End If Next Console.WriteLine("WorkerFunction is completed") End Sub End Class
Imports System.Threading Public Class ThreadPriority Public Shared worker1 As Thread Public Shared worker2 As Thread Public Shared Sub main() Console.WriteLine("Entering Sub Main()") worker1 = New Thread(AddressOf TestPriority1) worker2 = New Thread(AddressOf TestPriority2) worker1.Name = "TestPriority1()_Thread" worker2.Name = "TestPriority2()_Thread" worker2.Priority = Threading.ThreadPriority.Highest worker1.Start() worker2.Start() Console.ReadLine() End Sub Public Shared Sub TestPriority1() Console.WriteLine("-------------------") Console.WriteLine("Name:" & worker1.Name) Console.WriteLine("State:" & worker1.ThreadState.ToString) Console.WriteLine("Priority:" & worker1.Priority.ToString) End Sub Public Shared Sub TestPriority2() Console.WriteLine("===================") Console.WriteLine("Name:" & worker2.Name) Console.WriteLine("State:" & worker2.ThreadState.ToString) Console.WriteLine("Priority:" & worker2.Priority.ToString) End Sub End Class
Imports System.Threading Imports System.Text Public Class ThreadTimer Private Shared message As String Private Shared tmr As Timer Private Shared complete As Boolean Public Shared Sub main() Dim obj As New ThreadTimer Dim t As New Thread(AddressOf obj.GenerateText) t.Start() '1、启动产生文本的线程 Dim tmrCallBack As New TimerCallback(AddressOf obj.GetText) '--------------------------------------- 3、每5毫秒触发立即启动方法 tmr = New Timer(tmrCallBack, Nothing, TimeSpan.Zero, TimeSpan.FromMilliseconds(5)) Do If complete Then Exit Do '7、退出 Loop Console.WriteLine("Exiting Main...") Console.ReadLine() End Sub Public Sub GenerateText() '产生字符并接入末尾 Dim i As Integer Dim sb As StringBuilder = New StringBuilder For i = 1 To 10 Thread.Sleep(10) '2、模拟计算耗时 sb.Insert(sb.Length, " This is line ") sb.Insert(sb.Length, i.ToString) sb.Insert(sb.Length, vbCrLf) Next message = sb.ToString End Sub Public Sub GetText() '非空时输出字符 If message Is Nothing Then '4、无信息时,提示 Console.WriteLine(Now.Second.ToString & ":" & Now.Millisecond.ToString & " Not message,waiting...") Exit Sub End If Console.WriteLine("Message is :-------------------------------") Console.WriteLine(message) tmr.Dispose() '5、计时器释放,失效 complete = True '6、主程序循环标志退出 End Sub End Class注意:
当2处循环次数改为100次时,会发现message会输出两次(尽管代码只有一次),因为是5毫秒触发一次,当一次输出时(100行)时超过5毫秒,这第二次5毫秒后的触发又来了,所以有两次情况。当循环次数改为200时,会有三次message的输出,总之次数越长就会越混乱,最好的办法就是把计时器释放提到message之前,还没有输出就让其失效
8.线程生线程
一个线程可产生出多个线程,每个方法都可在单独的线程时间片中执行。例,先新添一类
Imports System.Threading Public Class Car Public Sub StartTheEngine() Console.WriteLine("Starting the engine!") Dim batt As Thread = New Thread(AddressOf CheckTheBattery) Dim fuel As Thread = New Thread(AddressOf CheckForFuel) Dim eng As Thread = New Thread(AddressOf CheckTheEngine) batt.Start() fuel.Start() eng.Start() UseTime() Console.WriteLine("Engine is ready!") End Sub Private Sub CheckTheBattery() Console.WriteLine("Checking the Battery!") UseTime() Console.WriteLine("Finished checking the Battery!") End Sub Private Sub CheckForFuel() Console.WriteLine("Checking for Fuel!") UseTime() Console.WriteLine("Fuel is available!") End Sub Private Sub CheckTheEngine() Console.WriteLine("Checking the engine!") UseTime() Console.WriteLine("Finished checking the engine!") End Sub Private Sub UseTime() For i As Integer = 1 To 100000000 Next End Sub End Class再主模块中如下(右为模式图):
Private Sub CheckTheEngine() Dim cke As New Engine cke.CheckTheEngine() End Sub并新加类Engine:
Imports System.Threading Public Class Engine Public Sub CheckTheEngine() Dim chk1 As Thread = New Thread(AddressOf Check1) Dim chk2 As Thread = New Thread(AddressOf Check2) chk1.Start() chk2.Start() Console.WriteLine("Checking the engine!") For count As Integer = 1 To 100000000 Next Console.WriteLine("Finished checking the engine!") End Sub Private Sub Check1() Console.WriteLine("Starting the engine check!!") For i As Integer = 1 To 100000000 Next Console.WriteLine("Finished engine check 1 !") End Sub Private Sub Check2() Console.WriteLine("Starting the engine check2!") For i As Integer = 1 To 100000000 Next Console.WriteLine("Finished engine check2!") End Sub End Class运行的结果:
标签:
原文地址:http://blog.csdn.net/dzweather/article/details/51596329