數學之路-vb.net並行計算(2),-vb.net並行計算

來源:互聯網
上載者:User

數學之路-vb.net並行計算(2),-vb.net並行計算

一、TLS種類

1)動態TLS

2)靜態TLS

靜態TLS的速度比動態TLS快,在編譯期就決定,需要定義一個靜態域來表示TLS資料,編譯器有足夠的資訊來在編譯期間內發射代碼,動態TLS需要通過一個或多個函數調用來獲得地址。

二、靜態TLS

我們可以線上程函數中使用static聲明一個靜態變數,這個變數會被所有使用這個函數的線程共用。比如,我們寫一個簡單的計算:

 200-1-2-....-20

其中減法部分有3個線程來完成,則意味著3個線程要共用一個臨時的計算結果

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
        Static jg As Integer = 200
        Dim temp As Integer
        Try
            For mynum = 1 To 20
                temp = jg
                jg -= mynum
                Console.WriteLine(data & ":" & Now.ToLongTimeString & "=>" & temp & "-" & mynum & ",計算結果為:" & jg)
                Thread.Sleep(1)
            Next
        Catch
            Console.WriteLine(data & ":" & Now.ToLongTimeString & "線程異常終止!")
            '終止線程
            Thread.CurrentThread.Abort()
        End Try
    End Sub
End Module

    Dim mynum As Integer
        Static jg As Integer = 200
        Dim temp As Integer
        Try
            For mynum = 1 To 20
                temp = jg
                jg -= mynum
                Console.WriteLine(data & ":" & Now.ToLongTimeString & "=>" & temp & "-" & mynum & ",計算結果為:" & jg)
                Thread.Sleep(1)
            Next

 

jg就是一個靜態域,被多個線程共用

運行結果如下



 

 

我們使用靜態TLS功能,讓jg成為一個執行緒區域變數,對每個線程而言,都是

在操作這個變數的副本

 

在某些多線程方案中,可能要為每個線程提供它自己的私人資料。 此類資料稱為“執行緒區域資料”。 在 .NET Framework 3.5 和更低版本中,可以將 ThreadStatic 特性應用於靜態變數以使其成為執行緒區域變數。 但是,使用 ThreadStatic 特性會導致細小的錯誤。 例如,即使基本的初始化語句也將導致該變數只在訪問它的第一個線程上進行初始化,如以下樣本中所示: 

<ThreadStaticAttribute> _
Shared counter As Integer

比如:

Imports System
Imports System.Threading


Class Test


    <MTAThread> _
    Shared Sub Main()


        For i As Integer = 1 To 3
            Dim newThread As New Thread(AddressOf ThreadData.ThreadStaticDemo)
            newThread.Start()
        Next i


    End Sub


End Class


Class ThreadData


    <ThreadStaticAttribute> _
    Shared threadSpecificData As Integer


    Shared Sub ThreadStaticDemo()


        ' Store the managed thread id for each thread in the static
        ' variable.
        threadSpecificData = Thread.CurrentThread.ManagedThreadId


        ' Allow other threads time to execute the same code, to show
        ' that the static data is unique to each thread.
        Thread.Sleep( 1000 )


        ' Display the static data.
        Console.WriteLine( "Data for managed thread {0}: {1}", _
            Thread.CurrentThread.ManagedThreadId, threadSpecificData )


    End Sub


End Class


' This code example produces output similar to the following:
'
'Data for managed thread 4: 4
'Data for managed thread 5: 5
'Data for managed thread 3: 3

在所有其他線程上,該變數將通過使用預設值(零)來進行初始化。

 

本部落格所有內容是原創,如果轉載請註明來源http://blog.csdn.net/myhaspl/
我們在.net 4.0也就是說vb.net 2010中,使用替代方案:

,可以使用 System.Threading.ThreadLocal(Of T) 類型建立基於執行個體的執行緒區域變數,此變數可通過您提供的 Action(Of T) 委託在所有線程上進行初始化。

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 ThreadLocal(Of Integer) = New ThreadLocal(Of Integer)(Function() 50)
        Try
            For mynum = 1 To 5
                jg.Value -= mynum
                Console.WriteLine(data & "  " & Now.ToLongTimeString & "=>" & (jg.Value + mynum) & "-" & mynum & ",計算結果為:" & jg.Value)
                Thread.Sleep(2)
            Next
        Catch
            Console.WriteLine(data & "  " & Now.ToLongTimeString & "線程異常終止!")
            '終止線程
            Thread.CurrentThread.Abort()
        End Try
    End Sub

End Module




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.