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

在以前的示例中,主线程都是以join的方式等待3个工作线程一起完成连续减法运算,现在我们用上节所说的WaitHandle类,我们使用其中的一个方法WaitHandle.WaitAll,这个方法能同时等待多个线程,只至所有等待对象都完成

msdn上的说明:

WaitHandle.WaitAll (WaitHandle[]), 等待指定数组中的所有元素都收到信号

代码如下:

 

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











Module Module1

    <MTAThread()> _
    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)






        '分别给每个线程创建一个AutoResetEvent
        Dim threadevent(2) As AutoResetEvent
        threadevent(0) = New AutoResetEvent(False)
        threadevent(1) = New AutoResetEvent(False)
        threadevent(2) = New AutoResetEvent(False)
        '执行线程
        mythread1.Start(threadevent(0))
        mythread2.Start(threadevent(1))
        mythread3.Start(threadevent(2))
        '等待线程执行完毕
        WaitHandle.WaitAll(threadevent)
        '线程执行完毕
        Console.WriteLine(Now.ToLongTimeString & "线程执行完毕!")

    End Sub
    Public Sub mythreadrun(ByVal threadevent As Object)
        Dim mynum As Double


        Static Dim jg As Double = 1000
        Static autoEvent As New AutoResetEvent(True)
        Try
            For mynum = 1 To 5 Step 0.1
                autoEvent.WaitOne()
                jg -= mynum
                Console.WriteLine(Thread.CurrentThread.Name & "==>" & Now.ToLongTimeString & "=>" & (jg + mynum) & "-" & mynum & ",计算结果为:" & jg)
                autoEvent.Set()
            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 & "线程运行完毕!")
            autoEvent.Set()
            CType(threadevent, AutoResetEvent).Set()
        End Try
    End Sub
End Module

  上述代码中,有几个要点:

1)要使用多线程套间(MTAThread ),以前我们使用的是单线程套间(STAThread),但WaitHandle.WaitAll要求

运动在MTAThread下

将<MTAThread()> 标志加上
Module Module1

    <MTAThread()> _
    Sub Main()

2)定义一个AutoResetEvent数组,并初始化

        Dim threadevent(2) As AutoResetEvent
        threadevent(0) = New AutoResetEvent(False)
        threadevent(1) = New AutoResetEvent(False)
        threadevent(2) = New AutoResetEvent(False)

 

3)将数组中的元素分别传给3个线程

        mythread1.Start(threadevent(0))
        mythread2.Start(threadevent(1))
        mythread3.Start(threadevent(2))

然后主线程等待这数组中的3个元素都为终止状态,即所有运算完成才终止

      Finally
            Console.WriteLine(Thread.CurrentThread.Name & "优先级:  " & Thread.CurrentThread.Priority.ToString)
            Console.WriteLine(Thread.CurrentThread.Name & "  " & Now.ToLongTimeString & "线程运行完毕!")
            autoEvent.Set()
            CType(threadevent, AutoResetEvent).Set()
最后在前面的例子中,如果一个线程计算发生错误,则可能会导致autoEvent.Set()没有执行,将会造成其它线程永久性等待。因此本节将autoEvent.Set()加入   Finally中



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值