标签:
线程可以处于一个或多个状态,由ThreadState枚举表示。使用Thread类中的一些方法后状态会随之变化。枚举成员如下:
线程的生存期如下:
一、线程睡眠
若线程想要访问的资源不可使用,只能期望隔段时间后,重新尝试讯问资源,这时就要让该线程睡眠等待,线程就会处WaitSleepJoin状态。
Imports System.Threading Public Class ThreadSleep Public Shared worker As Thread Public Shared worker2 As Thread Public Shared Sub Main() Console.WriteLine("Entering the Sub Main!") worker = New Thread(AddressOf Counter) worker2 = New Thread(AddressOf Counter2) worker2.Priority = ThreadPriority.Highest worker.Start() worker2.Start() Console.WriteLine("Exiting the Sub Main!") Console.ReadLine() End Sub Public Shared Sub Counter() Console.WriteLine("Entering Counter") For i As Integer = 1 To 50 Console.Write(i & " ") If i = 10 Then Console.WriteLine(" worker_线程暂停1秒...") worker.Sleep(1000) '等效于worker.Sleep(TimeSpan.FromSeconds(1)) Console.WriteLine("worker线程恢复:") End If Next Console.WriteLine() Console.WriteLine("Exiting Counter") End Sub Public Shared Sub Counter2() Console.WriteLine("Entering Countcr2") For i As Integer = 51 To 100 Console.Write(i & " ") If i = 70 Then Console.WriteLine(" worker2 线程暂停5秒...") worker2.Sleep(5000) Console.WriteLine("worker2线程恢复:") End If Next Console. WriteLine() Console.WriteLine("Exiting Counter2") End Sub End Class说明:由于worker2线程有较高优先权,所以先执行,至70时暂停5秒;worker较低优先权随后执行,至10时暂停1秒,后继续输出,直到5秒后worder2睡醒,再输出。同时注意,由于线程的优先都是受操作系统调控安排,可以看到worker线程的1、2先输出,继而是worder2的51、52等输出,故优先权并不是绝对意义上的优先。
Imports System.Threading Public Class Interrupt Public Shared sleeper As Thread Public Shared worker As Thread Public Shared Sub Main() Console.WriteLine("Entering the Sub Main!") sleeper = New Thread(AddressOf SleepingThread) worker = New Thread(AddressOf AwakeTheThread) sleeper.Start() worker.Start() Console.WriteLine("Exiting the Sub Main!") Console.ReadLine() End Sub Public Shared Sub SleepingThread() For i As Integer = 1 To 50 Console.Write(i & " ") If i = 10 Or i = 20 Or i = 30 Then Console.WriteLine("Going to sleep at: " & i) Try 'ThreadInterruptedException 在中断的线程中引发,但要在该线程阻塞之后才引发。 sleeper.Sleep(30) '这里易抛异常,用Try Catch ex As Exception '原因见4.2(原因http://blog.csdn.net/wguoyong/article/details/50918322) End Try End If Next End Sub Public Shared Sub AwakeTheThread() Dim i As Integer For i = 51 To 100 Console.Write(i & " ") If sleeper.ThreadState = System.Threading.ThreadState.WaitSleepJoin Then Console.WriteLine("Interrupting the sleeping thread") sleeper.Interrupt() End If Next End Sub End Class说明:这只是我们想象中让睡眠的线程中断唤醒,但实际上,中断唤醒并不是立即执行,加上线程是由操作系统在调配,更增加了不可控性。上面程序最易出现的就是抛出异常,由于延迟执行中断,中断可能出现在方法AwakeTheThread中任意一段代码中。
Imports System.Threading Public Class JoiningThread Public Shared SecondThread As Thread Public Shared FirstThread As Thread Shared Sub First() '第一线程 For i As Integer = 1 To 50 Console.Write(i & " ") Next Console.WriteLine("First finish!") End Sub Shared Sub Second() '第二线程 FirstThread.Join() '连接到第一线程,只有第一线程终止后,第二线程才能接手运行 Console.WriteLine("Second start...") For i As Integer = 51 To 100 Console.Write(i & " ") Next End Sub Public Shared Sub Main() FirstThread = New Thread(AddressOf First) SecondThread = New Thread(AddressOf Second) FirstThread.Start() SecondThread.Start() Console.ReadLine() End Sub End Class
Imports System.Threading Imports System.IO Public Class Form1 Dim searchTerm As String Dim totalFiles As Integer Dim blnSimple As Boolean Delegate Sub ClearText() Delegate Sub AddText(ByVal txt As String) Private Sub btnSimple_Click(sender As Object, e As EventArgs) Handles btnSimple.Click Search() blnSimple = True End Sub Public Sub Search() Invoke(New ClearText(AddressOf ClearList)) searchTerm = TextBox1.Text totalFiles = 0 SearchDirectory("F:\Tools") End Sub Public Sub SearchDirectory(ByVal Path As String) Dim di As New DirectoryInfo(Path) Dim f() As FileInfo = di.GetFiles(searchTerm) Dim myFile As FileInfo For Each myFile In f If ListBox1.InvokeRequired = True Then Invoke(New AddText(AddressOf AddList), myFile.FullName) End If Next Dim d() As DirectoryInfo = di.GetDirectories(searchTerm) Dim myDir As DirectoryInfo For Each myDir In d SearchDirectory(myDir.FullName) Next End Sub Private Sub btnMutli_Click(sender As Object, e As EventArgs) Handles btnMutli.Click Dim t As New Thread(AddressOf Search) blnSimple = False t.Start() End Sub Private Sub ClearList() ListBox1.Items.Clear() End Sub Private Sub AddList(ByVal it As String) ListBox1.Items.Add(it) End Sub End Class说明:用SimpleThread按钮运行,明显有停滞出现;但用多线程MultiThread却没任何感觉,很流畅。另外由于其它线程无法直接操作控件:
Imports System.Threading Public Class ThreadOrder Shared tl As Thread Shared t2 As Thread Public Shared Sub WriteFinished(ByVal threadName As String) Select Case threadName Case "Tl" Console.WriteLine() Console.WriteLine("Tl Finished") Case "T2" Console.WriteLine() Console.WriteLine("T2 Finished”) End Select End Sub Public Shared Sub Main() tl = New Thread(AddressOf Increment) t2 = New Thread(AddressOf Increment) tl.Name = "Tl" t2.Name = "T2" tl.Start() '1、t1先执行 t2.Start() Console.ReadLine() End Sub Public Shared Sub Increment() For i As Long = 1 To 1000000 If i Mod 100000 = 0 Then Console.Write("{" + Thread.CurrentThread.Name + "}") End If Next WriteFinished(Thread.CurrentThread.Name) End Sub End Class说明:尽管t1先执行,但并不一定它最先走进CPU。线程由于是操作系统来调配,顺序变得不可控。下面三次运行的结果都不相同,注意谁先显示,谁先结束。
Public Shared Sub SendAllEmail() For i As Integer = 0 To al.Count - 1 Dim t As Thread = LoopingThreads.CreateEmail(AddressOf Mailer.MailMethod, al(i), "johndoe@somewhere.com", "Threading in a loop", "Mail Example") t.Start() t.Join(Timeout.Infinite) Next End Sub上面依次逐一发送邮件,但如果有一个发生不成功,会怎么样?另外看似是队列进入,但能不能到达CPU谁也没法说清,如果皇帝就是cpu,妃子就是线程,若妃子们都打扮好了(线程Start),就等待着皇帝宠幸了。但皇帝决定翻牌宠幸谁呢?皇帝可不管谁先打扮好(Start),他看中那个就宠幸谁先,这个是皇帝决定的。尽管皇家有祖制(线程先后轮询规则),但毕竟皇上主观性太大,无法确定谁先谁后。
标签:
原文地址:http://blog.csdn.net/dzweather/article/details/51604964