VB.NET并行与分布式编程(6)-线程与内核同步[13]
Imports System Imports System.Threading Imports System.Diagnostics Imports System.Diagnostics.ThreadState Module Module1 '生产者线程和消费者线程共同操作的数 Dim mycounts As Integer = 0 '定义一个互斥对象,用于解决生产线程和消费线程操作mycounts变量的同步问题 Dim mymutex As New Mutex Sub Main() Dim mythread1 As Thread Dim mythread2 As Thread Dim mythread3 As Thread Dim mymakethread As Thread '创建线程对象 mythread1 = New Thread(AddressOf mythreadrun) mythread2 = New Thread(AddressOf mythreadrun) mythread3 = New Thread(AddressOf mythreadrun) mymakethread = New Thread(AddressOf mymake) Console.WriteLine(Now.ToLongTimeString & "线程对象创建完毕,开始执行线程") '定义一个信号量 Dim mysemaphore As New Semaphore(0, 5) '执行线程 mymakethread.Start(mysemaphore) mythread1.Start(mysemaphore) mythread2.Start(mysemaphore) mythread3.Start(mysemaphore) '等待线程完成 mymakethread.Join() mythread1.Join() mythread2.Join() mythread3.Join() '线程执行完毕 Console.WriteLine(Now.ToLongTimeString & "主线程执行完毕!") mysemaphore.Close() End Sub Public Sub mymake(ByVal semaphore As Semaphore) '生产者线程 Dim i As Integer '生产者完成生产5次就退出生产者线程,这里假定就输出一行 For i = 1 To 5 Console.WriteLine("{0}号生产者线程等待运行", Thread.CurrentThread.ManagedThreadId) '增加1个信号量 semaphore.Release() mymutex.WaitOne() mycounts += 1 Console.WriteLine("{0}号生产者线程运行,目前数量为:{1}", Thread.CurrentThread.ManagedThreadId, mycounts) mymutex.ReleaseMutex() Thread.Sleep(1) Next End Sub Public Sub mythreadrun(ByVal semaphore As Semaphore) '消费者线程,这里假定就输出一行 Try Console.WriteLine("{0}号消费者线程等待运行", Thread.CurrentThread.ManagedThreadId) '将信号量的计数减1,如果此时信号量为0,则等待 semaphore.WaitOne() mymutex.WaitOne() mycounts -= 1 Console.WriteLine("{0}号消费者线程运行,目前数量为:{1}", Thread.CurrentThread.ManagedThreadId, mycounts) mymutex.ReleaseMutex() Thread.Sleep(15) Catch Console.WriteLine("{0},{1}号线程异常终止!", Now.ToLongTimeString, Thread.CurrentThread.ManagedThreadId) '终止线程 Thread.CurrentThread.Abort() Finally Console.WriteLine("{0},{1}消费者线程运行完成!", Now.ToLongTimeString, Thread.CurrentThread.ManagedThreadId) End Try End Sub End Module
为了更直观地体验信号量的功能,我们加入一个mycounts变量,生产线程负责这个变量每次增加1,而消费者线程负责这个变量每次减少1, 为了解决同步问题,我们必须要引入互斥,用互斥来保护变量只能同时被一个线程操作 ,代码如上。
这个代码有几个地方需要注意
1、 定义一个互斥对象,用于解决生产线程和消费线程操作mycounts变量的同步问题
Dim mymutex As New Mutex
2、主线程执行完毕,关闭信号量
Console.WriteLine(Now.ToLongTimeString & "主线程执行完毕!")
mysemaphore.Close()
3、将对mycounts变量的操作放在互斥体的保护下
mymutex.WaitOne()
mycounts += 1
Console.WriteLine("{0}号生产者线程运行,目前数量为:{1}", Thread.CurrentThread.ManagedThreadId, mycounts)
mymutex.ReleaseMutex()
其中, mymutex.WaitOne()等待,而 mymutex.ReleaseMutex()释放
此外,WaitHandle.SignalAndWait 方法在解决复杂的同步问题,尤其是防止死锁很有帮助 ,对于同时操作信号量和互斥体,可以使以原子方式释放互斥体并在事件上等待。WaitHandle.SignalAndWait 方法向一个WaitHandle 发出信号并等待另一个,如果信号和等待都成功完成,则为 true;如果等待没有完成,则此方法不返回,第一个参数为信号,第二个参数为等待