Mathematical Road-vb.net Parallel Computing (6)

Source: Internet
Author: User



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










' Create thread Object
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"
' Set priority
Mythread1. Priority = Threadpriority.lowest
Mythread2. Priority = Threadpriority.highest
Mythread3. Priority = Threadpriority.normal










Console.WriteLine (Now.tolongtimestring & "Thread object creation completed, start thread Execution")


' Thread of execution
Mythread1. Start ()
Mythread2. Start ()
Mythread3. Start ()








' Wait for thread to finish
Mythread1. Join ()
Mythread2. Join ()
Mythread3. Join ()




' Thread execution is complete
Console.WriteLine (now.tolongtimestring & "thread execution finished!")
End Sub
Public Sub Mythreadrun ()
Dim Mynum as Integer














' Allocate a new slot, this slot holds the thread local data, the slot name is called
' Myjg, the name must be unique
Dim JG as LocalDataStoreSlot
JG = Thread.getnameddataslot ("Myjg")
Thread.setdata (JG, 100)
Try
For mynum = 1 to 30
Thread.setdata (JG, Thread.getdata (JG)-Mynum)
Console.WriteLine (Thread.CurrentThread.Name & "==>" & now.tolongtimestring & "=" & ( Thread.getdata (JG) + Mynum) & "-" & mynum & ", calculated as:" & Thread.getdata (JG))
Thread.Sleep (50)
Next
Catch
Console.WriteLine (Thread.CurrentThread.Name & "" & Now.tolongtimestring & "thread abnormally terminated!")
' Terminate thread
Thread.CurrentThread.Abort ()
Finally
Console.WriteLine (Thread.CurrentThread.Name & "" & Now.tolongtimestring & "thread runs!")
Console.WriteLine (Thread.CurrentThread.Name & "" & Thread.CurrentThread.Priority.ToString)
Thread.freenameddataslot ("Myjg")
End Try


End Sub
End Module
This blog all content is original, if reproduced please indicate sourcehttp://blog.csdn.net/myhaspl/


There are several ways to set the thread priority

1) Set priority as shown in the code above
Mythread1. Priority = Threadpriority.lowest
Mythread2. Priority = Threadpriority.highest
Mythread3. Priority = Threadpriority.normal
This approach allows the CLR to do some extra bookkeeping so that when a thread returns to the thread pool with a higher priority than normal, these bookkeeping information is

Restore the priority level.
ThreadPriority enumeration values are listed in the following ways

Lowest can schedule thread to be behind threads that have any other priority.
BelowNormal can schedule the thread to precede a thread with the Normal priority, before the threads with the Lowest priority.
Normal can schedule thread to precede a thread with a abovenormal priority, before threads with a belownormal priority. By default, threads have the Normal priority.
AboveNormal can schedule the thread to precede a thread with a highest priority, before the threads with the Normal priority.
Highest can schedule thread to precede threads that have any other priority.

threadpriority defines all possible values for a set of thread priorities. The thread priority specifies a line threads relative priority for another thread.

each thread has a priority assigned to it. threads created within the runtime are initially assigned the Normal priority, and threads created outside the runtime retain their previous priority when they enter the runtime. The Priority property of the thread can be obtained and set by accessing it.

schedules the execution of threads based on the priority of the thread. The scheduling algorithm used to determine the order of thread execution varies with the operating system. the operating system can also dynamically adjust the priority of a thread when the user interface's focus moves between the foreground and the background.


2) Set priority by System.Diagnostics.ProcessThread, it can set more priority than the 1th method

idle  specifies the idle priority level. It is the lowest possible priority value for all threads, regardless of the value associated with the ProcessPriorityClass.   
 Lowest  specifies the lowest priority. This is two levels lower than the normal priority of the associated ProcessPriorityClass.   
 BelowNormal  specifies a lower priority than the normal priority of the associated ProcessPriorityClass.   
 Normal  Specifies the normal priority of the associated ProcessPriorityClass.   
 AboveNormal  specifies a priority higher than the normal priority of the associated ProcessPriorityClass.   
 Highest  specifies the highest priority. This is two levels higher than the normal priority of the associated ProcessPriorityClass.   
 TimeCritical  Specify a time-critical priority. This is the highest priority in all threads, regardless of the value of the associated ProcessPriorityClass

Dim myprocess as Process = Process.getcurrentprocess ()
Dim value as Processthreadcollection
Dim PT as ProcessThread
Value = myprocess. Threads
For each PT in value
Pt. Prioritylevel = Threadprioritylevel.abovenormal
Next

In addition, you can set the switch for thread dynamic priority adjustment

Processthread.priorityboostenabled

When Priorityboostenabled is true, the system temporarily promotes the priority of the thread as long as the associated process leaves the waiting state. This action prevents other processes from interrupting the processing of the current thread. The priorityboostenabled setting affects all existing threads and all subsequent threads created by the process. To restore normal behavior, set the Priorityboostenabled property to False.

Priorityboostenabled is valid only if the thread is running in one of the enumeration values that set PriorityClass to dynamic precedence (Normal, high, or RealTime).

4. Sleep and obstruction

Sleep (Int32) suspends the current thread for a specified time. Note that the parameter isthe number of milliseconds the thread is blocked

Imports System.Threading


Class Example


Shared Sub Main ()


For i as Integer = 0 to 4
Console.WriteLine ("Sleep for 2 seconds.")
Thread.Sleep (2000)
Next


Console.WriteLine ("Main thread exits.")
End Sub
End Class


Sleep (TimeSpan) blocks the current thread for a specified time. Here is an example of blocking on MSDN

Imports System.Threading


Class Example


Shared Sub Main ()


Dim interval as New TimeSpan (0, 0, 2)


For i as Integer = 0 to 4
Console.WriteLine ("Sleep for 2 seconds.")
Thread.Sleep (interval)
Next


Console.WriteLine ("Main thread exits.")
End Sub
End Class


Mythread1. Start (myprocess)
Mythread2. Start (myprocess)
Mythread3. Start (myprocess)
Mythread1. Suspend ()
Mythread2. Suspend ()
Mythread3. Suspend ()


Mythread1. Resume ()
Mythread2. Resume ()
Mythread3. Resume ()


Mathematical Road-vb.net Parallel Computing (6)

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.