Mathematical path -vb.net parallel computing (1), -vb.net parallel computing

Source: Internet
Author: User

Mathematical path -vb.net parallel computing (1), -vb.net parallel computing
Imports System
Imports System. Threading




Module Module1


Sub Main ()


Dim mythread1 As Thread
Dim mythread2 As Thread
Dim mythread3 As Thread
'Create a thread object
Mythread1 = New Thread (AddressOf mythreadrun)
Mythread2 = New Thread (AddressOf mythreadrun)
Mythread3 = New Thread (AddressOf mythreadrun)
Console. WriteLine (Now. ToLongTimeString & "after the thread object is created, start the execution thread ")
'Execution thread
Mythread1.Start ("thread 1 ")
Mythread2.Start ("thread 2 ")
Mythread3.Start ("thread 3 ")
'Wait for the thread to finish
Mythread1.Join ()
Mythread2.Join ()
Mythread3.Join ()
'Thread execution completed
Console. WriteLine (Now. ToLongTimeString & "finished thread execution! ")
End Sub
Public Sub mythreadrun (ByVal data As Object)
Console. WriteLine (data & "," & Now. ToLongTimeString & "run ")
End Sub

End Module


All content of this blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/

 

Dim mythread1 As Thread
Initializes a new instance of the Thread class and specifies the delegate that allows the object to be passed to the Thread when the Thread starts.

The thread is not executed at creation. To schedule the thread for execution, call the start method.

The code above creates three thread objects and then calls their start method to execute them.

Call the join method and wait until they are executed.

You can create a new Console application in VB. NET, and then write the above Code in the module.

 

New Thread (AddressOf mythreadrun)
The constructor must be passed as a thread-executed function as a parameter.

 

Mythread1.Start ("thread 1 ")
The start method can input parameters to the thread during execution.

 

Imports System
Imports System. Threading

The program needs to import the above namespace



& Nbsp; Imports System
Imports System. Threading




Module Module1


Sub Main ()


Dim mythread1 As Thread
Dim mythread2 As Thread
Dim mythread3 As Thread
'Create a thread object
Mythread1 = New Thread (AddressOf mythreadrun)
Mythread2 = New Thread (AddressOf mythreadrun)
Mythread3 = New Thread (AddressOf mythreadrun)
Console. WriteLine (Now. ToLongTimeString & "after the thread object is created, start the execution thread ")


'Execution thread
Mythread1.Start ("thread 1 ")
Mythread2.Start ("thread 2 ")
Mythread3.Start ("thread 3 ")
'Wait for the thread to finish
Mythread1.Join ()
Mythread2.Join ()
Mythread3.Join ()
'Thread execution completed
Console. WriteLine (Now. ToLongTimeString & "finished thread execution! ")
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 & "Calculation Result:" & jg)
Thread. Sleep (1)
Next
Catch
Console. WriteLine (data & ":" & Now. ToLongTimeString & "thread termination exception! ")
'Terminate the thread
Thread. CurrentThread. Abort ()
End Try
End Sub
End Module

 

Calculate the value of 1000/mynum in the Thread. The value of mynum decreases from 20 to 0, and an exception will occur when the value is 0. Call Thread. CurrentThread. Abort ()
Terminate a thread

Try
For mynum = 20 To-20 Step-1
Jg = 1000/mynum
Console. WriteLine (data & ":" & Now. ToLongTimeString & ", 1000/" & mynum & "Calculation Result:" & jg)
Thread. Sleep (1)
Next
Catch
Console. WriteLine (data & ":" & Now. ToLongTimeString & "thread termination exception! ")
'Terminate the thread
Thread. CurrentThread. Abort ()
End Try


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.