VB.NET并行与分布式编程(6)-线程与内核同步[17]
ReaderWriterLockSlim.EnterUpgradeableReadLock 方法尝试进入可升级模式锁定状态。
此方法在调用线程进入锁定状态之前一直处于阻塞状态,因此可能永远都不返回。 使用 TryEnterUpgradeableReadLock 方法阻塞指定的间隔时间,如果调用线程在此间隔期间并未进入可升级模式,将返回。
如果线程在读取模式下经常访问受 ReaderWriterLockSlim 保护的资源,则使用可升级模式;但在满足特定条件时可能需要进入写入模式。 处于可升级模式的线程可以降级为读取模式或升级为写入模式。
任意给定时间都只有一个线程可以进入可升级模式。 如果某个线程处于可升级模式,而且没有线程正等待进入写入模式,则即使有线程等待进入可升级模式,任意数量的其他线程也都可以进入读取模式。
如果一个或多个线程正等待进入写入模式,调用 EnterUpgradeableReadLock 方法的线程在这些线程超时或进入写入模式然后又从中退出之前,将一直受阻。
效果如下: Imports System
Imports System.Threading
Imports System.Diagnostics
Imports System.Diagnostics.ThreadState
Module Module1
Dim myLock As New ReaderWriterLockSlim()
Dim jg As Double = 300
Sub Main()
Dim mythread1 As Thread
Dim mythread2 As Thread
Dim mythread3 As Thread
'创建线程对象
mythread1 = New Thread(AddressOf mythreadrun)
mythread2 = New Thread(AddressOf mythreadrun)
mythread3 = New Thread(AddressOf mythreadread)
mythread1.Name = "thread_1"
mythread2.Name = "thread_2"
mythread3.Name = "thread_3"
Console.WriteLine(Now.ToLongTimeString & "线程对象创建完毕,开始执行线程")
Dim myprocess As Process = Process.GetCurrentProcess()
Console.WriteLine("进程名称:" & myprocess.ProcessName)
'执行线程
mythread3.Start()
mythread2.Start()
mythread1.Start()
'等待线程完成
mythread1.Join()
mythread2.Join()
mythread3.Join()
'线程执行完毕
Console.WriteLine(Now.ToLongTimeString & "线程执行完毕!")
End Sub
Public Sub mythreadrun()
Dim mynum As Double
Try
For mynum = 1 To 100
'等待升级锁
Console.WriteLine(Thread.CurrentThread.Name & "===>" & Now.ToLongTimeString & "等待获得升级锁")
If myLock.TryEnterUpgradeableReadLock(3) Then
'获得升级锁
Console.WriteLine(Thread.CurrentThread.Name & " 获得升级锁")
'获得写锁,执行升级处理
myLock.EnterWriteLock()
jg -= mynum
Console.WriteLine(Thread.CurrentThread.Name & "获得写锁==>" & Now.ToLongTimeString & "=>" & (jg + mynum) & "-" & mynum & ",计算结果为:" & jg)
Thread.Sleep(13)
'退出写锁
myLock.ExitWriteLock()
Console.WriteLine(Thread.CurrentThread.Name & "退出写锁==>" & Now.ToLongTimeString)
'退出升级锁
myLock.ExitUpgradeableReadLock()
Console.WriteLine(Thread.CurrentThread.Name & "退出升级锁==>" & Now.ToLongTimeString)
If jg < 0 Then
Exit For
End If
Else
Console.WriteLine(Thread.CurrentThread.Name & "获得升级锁失败")
End If
Thread.Sleep(16)
Next
Catch
Console.WriteLine(Thread.CurrentThread.Name & " " & Now.ToLongTimeString & "线程异常终止!")
'终止线程
Thread.CurrentThread.Abort()
Finally
Console.WriteLine(Thread.CurrentThread.Name & "优先级: " & Thread.CurrentThread.Priority.ToString)
Console.WriteLine(Thread.CurrentThread.Name & " " & Now.ToLongTimeString & "线程运行完毕!")
End Try
End Sub
Public Sub mythreadread()
While jg > 0
'获得升级锁
Console.WriteLine(Thread.CurrentThread.Name & "===>" & Now.ToLongTimeString & "等待获得升级锁...")
If myLock.TryEnterUpgradeableReadLock(5) Then
Console.WriteLine(Thread.CurrentThread.Name & " 获得升级锁" & Now.ToLongTimeString)
'获得读锁,执行降级处理
If myLock.TryEnterReadLock(5) Then
'退出升级锁
Console.WriteLine(Thread.CurrentThread.Name & " 退出升级锁" & Now.ToLongTimeString)
Console.WriteLine(Thread.CurrentThread.Name & "==>" & Now.ToLongTimeString & " 获得读锁,当前计算值为{0}", jg)
myLock.ExitUpgradeableReadLock()
Thread.Sleep(15)
myLock.ExitReadLock()
Console.WriteLine(Thread.CurrentThread.Name & " 退出读锁和升级锁" & Now.ToLongTimeString)
Else
Console.WriteLine(Thread.CurrentThread.Name & " 获得读锁失败" & Now.ToLongTimeString)
End If
Else
Console.WriteLine(Thread.CurrentThread.Name & " 获得升级锁失败" & Now.ToLongTimeString)
End If
Thread.Sleep(10)
End While
End Sub
End Module