vb.net2019-多线程并行计算(1)

别说谁变了你拦得住时间么 提交于 2019-12-27 02:47:53
Imports System
Imports System.Threading




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)
        Console.WriteLine(Now.ToLongTimeString & "线程对象创建完毕,开始执行线程")
        '执行线程
        mythread1.Start("线程1")
        mythread2.Start("线程2")
        mythread3.Start("线程3")
        '等待线程完成
        mythread1.Join()
        mythread2.Join()
        mythread3.Join()
        '线程执行完毕
        Console.WriteLine(Now.ToLongTimeString & "线程执行完毕!")
    End Sub
    Public Sub mythreadrun(ByVal data As Object)
        Console.WriteLine(data & "," & Now.ToLongTimeString & "执行")
    End Sub

End Module

Dim mythread1 As Thread
初始化Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。

线程不会在创建时开始执行。 若要为执行而调度线程,调用start方法。

上面代码创建了3个thread对象,然后调用它们的start方法执行它们

调用join方法等它们执行完毕

可在VB.NET中建立一个新的Console应用程序,然后在模块中编写以上代码

New Thread(AddressOf mythreadrun)
构造函数中必须将要做为线程执行的函数做为参数传入

mythread1.Start(“线程1”)
start方法可以在执行线程时,向其传入参数

Imports System
Imports System.Threading

程序需要导入以上命名空间

Imports System
Imports System.Threading




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)
        Console.WriteLine(Now.ToLongTimeString & "线程对象创建完毕,开始执行线程")


        '执行线程
        mythread1.Start("线程1")
        mythread2.Start("线程2")
        mythread3.Start("线程3")
        '等待线程完成
        mythread1.Join()
        mythread2.Join()
        mythread3.Join()
        '线程执行完毕
        Console.WriteLine(Now.ToLongTimeString & "线程执行完毕!")
    End Sub
    Public Sub mythreadrun(ByVal data As Object)
        Dim mynum As Integer
        Dim jg As Integer
        mynum = 20
        Try
            For mynum = 20 To -20 Step -1
                jg = 1000 / mynum
                Console.WriteLine(data & ":" & Now.ToLongTimeString & ",1000 / " & mynum & "计算结果为:" & jg)
                Thread.Sleep(1)
            Next
        Catch
            Console.WriteLine(data & ":" & Now.ToLongTimeString & "线程异常终止!")
            '终止线程
            Thread.CurrentThread.Abort()
        End Try
    End Sub
End Module

在线程中计算1000/mynum的值,其中mynum从20开始递减,到0时会出现除0异常,调用Thread.CurrentThread.Abort()
完成终止线程的操作

Try
    For mynum = 20 To -20 Step -1
        jg = 1000 / mynum
        Console.WriteLine(data & ":" & Now.ToLongTimeString & ",1000 / " & mynum & "计算结果为:" & jg)
        Thread.Sleep(1)
    Next
Catch
    Console.WriteLine(data & ":" & Now.ToLongTimeString & "线程异常终止!")
    '终止线程
    Thread.CurrentThread.Abort()
End Try
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!