欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

VB.NET并行与分布式编程(6)-线程与内核同步[14]

程序员文章站 2022-04-29 10:55:38
...
Imports System
Imports System.Threading
Imports System.Diagnostics
Imports System.Diagnostics.ThreadState











Module Module1

    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 mythreadrun)
        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)
        '执行线程
        mythread1.Start(myprocess)
        mythread2.Start(myprocess)
        mythread3.Start(myprocess)





        '等待线程完成
        mythread1.Join()
        mythread2.Join()
        mythread3.Join()


        '线程执行完毕
        Console.WriteLine(Now.ToLongTimeString & "线程执行完毕!")

    End Sub
    Public Sub mythreadrun(ByVal myprocess As Object)
        Dim mynum As Double
        Static Dim mymonitorobj As New Object



        Static Dim jg As Double = 100
        Try
            For mynum = 1 To 10
                '隐式获取监视器
                SyncLock mymonitorobj
                    jg -= mynum
                    Console.WriteLine(Thread.CurrentThread.Name & "==>" & Now.ToLongTimeString & "=>" & (jg + mynum) & "-" & mynum & ",计算结果为:" & jg)
                End SyncLock
                Thread.Sleep(0)
            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
End Module

 上面的代码我们使用监视器

1)监视器的定义

  Static Dim mymonitorobj As New Object
2)使用 SyncLock关键字隐式获取监视器,制造互斥的临界域
                SyncLock mymonitorobj
                    jg -= mynum
                    Console.WriteLine(Thread.CurrentThread.Name & "==>" & Now.ToLongTimeString & "=>" & (jg + mynum) & "-" & mynum & ",计算结果为:" & jg)
                End SyncLock
此外,我们可以直接使用Monitor.enter方法和Monitor.exit方法

 

 Monitor.Enter(mymonitorobj)
                jg -= mynum
                Console.WriteLine(Thread.CurrentThread.Name & "==>" & Now.ToLongTimeString & "=>" & (jg + mynum) & "-" & mynum & ",计算结果为:" & jg)
                Monitor.Exit(mymonitorobj)

 

代码如下

Imports System
Imports System.Threading
Imports System.Diagnostics
Imports System.Diagnostics.ThreadState











Module Module1

    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 mythreadrun)
        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)
        '执行线程
        mythread1.Start(myprocess)
        mythread2.Start(myprocess)
        mythread3.Start(myprocess)





        '等待线程完成
        mythread1.Join()
        mythread2.Join()
        mythread3.Join()


        '线程执行完毕
        Console.WriteLine(Now.ToLongTimeString & "线程执行完毕!")

    End Sub
    Public Sub mythreadrun(ByVal myprocess As Object)
        Dim mynum As Double
        Static Dim mymonitorobj As New Object



        Static Dim jg As Double = 100
        Try
            For mynum = 1 To 10
                '使用 监视器
                Monitor.Enter(mymonitorobj)
                jg -= mynum
                Console.WriteLine(Thread.CurrentThread.Name & "==>" & Now.ToLongTimeString & "=>" & (jg + mynum) & "-" & mynum & ",计算结果为:" & jg)
                Monitor.Exit(mymonitorobj)

                Thread.Sleep(0)
            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
End Module

 上述方法将阻塞等待线程, 我们可以使用TryEnter来避免阻塞,在等待的同时做一些其它工作

 

 

  '使用 监视器,同时为避免阻塞,在等待的过程中做其它工作
                While Not Monitor.TryEnter(mymonitorobj)
                    Console.WriteLine(Thread.CurrentThread.Name & "正在等待.....")
                End While
                Try
                    jg -= mynum
                    Console.WriteLine(Thread.CurrentThread.Name & "==>" & Now.ToLongTimeString & "=>" & (jg + mynum) & "-" & mynum & ",计算结果为:" & jg)
                Finally
                    Monitor.Exit(mymonitorobj)
                End Try


                Thread.Sleep(0)

 

 

完整代码如下:

Imports System
Imports System.Threading
Imports System.Diagnostics
Imports System.Diagnostics.ThreadState











Module Module1

    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 mythreadrun)
        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)
        '执行线程
        mythread1.Start(myprocess)
        mythread2.Start(myprocess)
        mythread3.Start(myprocess)





        '等待线程完成
        mythread1.Join()
        mythread2.Join()
        mythread3.Join()


        '线程执行完毕
        Console.WriteLine(Now.ToLongTimeString & "线程执行完毕!")

    End Sub
    Public Sub mythreadrun(ByVal myprocess As Object)
        Dim mynum As Double
        Static Dim mymonitorobj As New Object



        Static Dim jg As Double = 100
        Try
            For mynum = 1 To 10
                '使用 监视器,同时为避免阻塞,在等待的过程中做其它工作
                While Not Monitor.TryEnter(mymonitorobj)
                    Console.WriteLine(Thread.CurrentThread.Name & "正在等待.....")
                End While
                Try
                    jg -= mynum
                    Console.WriteLine(Thread.CurrentThread.Name & "==>" & Now.ToLongTimeString & "=>" & (jg + mynum) & "-" & mynum & ",计算结果为:" & jg)
                Finally
                    Monitor.Exit(mymonitorobj)
                End Try


                Thread.Sleep(0)
            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
End Module

 

 

效果如下:


VB.NET并行与分布式编程(6)-线程与内核同步[14]
            
    
    博客分类: .net&silverlight  
 

 

 

  • VB.NET并行与分布式编程(6)-线程与内核同步[14]
            
    
    博客分类: .net&silverlight  
  • 大小: 73.5 KB