當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET Parallel.ForEach方法代碼示例

本文整理匯總了VB.NET中System.Threading.Tasks.Parallel.ForEach方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Parallel.ForEach方法的具體用法?VB.NET Parallel.ForEach怎麽用?VB.NET Parallel.ForEach使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Threading.Tasks.Parallel的用法示例。


在下文中一共展示了Parallel.ForEach方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: ForEachDemo

' 導入命名空間
Imports System.Threading
Imports System.Threading.Tasks

Module ForEachDemo

    ' Demonstrated features:
    '   Parallel.ForEach()
    '   Thread-local state
    ' Expected results:
    '   This example sums up the elements of an int[] in parallel.
    '   Each thread maintains a local sum. When a thread is initialized, that local sum is set to 0.
    '   On every iteration the current element is added to the local sum.
    '   When a thread is done, it safely adds its local sum to the global sum.
    '   After the loop is complete, the global sum is printed out.
    ' Documentation:
    '   http://msdn.microsoft.com/library/dd990270(VS.100).aspx
    Private Sub ForEachDemo()
        ' The sum of these elements is 40.
        Dim input As Integer() = {4, 1, 6, 2, 9, 5, _
        10, 3}
        Dim sum As Integer = 0

        Try
            ' source collection
            Parallel.ForEach(input,
                             Function()
                                 ' thread local initializer
                                 Return 0
                             End Function,
                             Function(n, loopState, localSum)
                                 ' body
                                 localSum += n
                                 Console.WriteLine("Thread={0}, n={1}, localSum={2}", Thread.CurrentThread.ManagedThreadId, n, localSum)
                                 Return localSum
                             End Function,
                             Sub(localSum)
                                 ' thread local aggregator
                                 Interlocked.Add(sum, localSum)
                             End Sub)

            Console.WriteLine(vbLf & "Sum={0}", sum)
        Catch e As AggregateException
            ' No exception is expected in this example, but if one is still thrown from a task,
            ' it will be wrapped in AggregateException and propagated to the main thread.
            Console.WriteLine("Parallel.ForEach has thrown an exception. THIS WAS NOT EXPECTED." & vbLf & "{0}", e)
        End Try
    End Sub


End Module
開發者ID:VB.NET開發者,項目名稱:System.Threading.Tasks,代碼行數:51,代碼來源:Parallel.ForEach

示例2: Example

' 導入命名空間
Imports System.IO
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim task As Task(Of String) = ReadCharacters(".\CallOfTheWild.txt")
      Dim text As String = task.Result
      
      Dim nVowels As Integer = 0
      Dim nNonWhiteSpace As Integer = 0
      Dim obj As New Object()

      Dim result As ParallelLoopResult = Parallel.ForEach(text, 
                                                          Sub(ch)
                                                             Dim uCh As Char = Char.ToUpper(ch)
                                                             If "AEIOUY".IndexOf(uCh) >= 0 Then
                                                                SyncLock obj
                                                                   nVowels += 1
                                                                End SyncLock
                                                             End If
                                                             If Not Char.IsWhiteSpace(uCh) Then
                                                                SyncLock obj
                                                                   nNonWhiteSpace += 1
                                                                End SyncLock   
                                                             End If
                                                          End Sub)
      Console.WriteLine("Total characters:      {0,10:N0}", text.Length)
      Console.WriteLine("Total vowels:          {0,10:N0}", nVowels)
      Console.WriteLine("Total non-white space:  {0,10:N0}", nNonWhiteSpace)
   End Sub
   
   Private Async Function ReadCharacters(fn As String) As Task(Of String)
      Dim text As String
      Using sr As New StreamReader(fn)
         text = Await sr.ReadToEndAsync()
      End Using
      Return text
   End Function
End Module
' The output from the example resembles the following:
'       Total characters:         198,548
'       Total vowels:              58,421
'       Total non-white space:     159,461
開發者ID:VB.NET開發者,項目名稱:System.Threading.Tasks,代碼行數:44,代碼來源:Parallel.ForEach

示例3: RangePartitionerDemo

' 導入命名空間
Imports System.Collections.Concurrent
Imports System.Threading
Imports System.Threading.Tasks

Module RangePartitionerDemo

    Sub Main()
        Dim sw As Stopwatch = Nothing

        Dim sum As Long = 0
        Dim SUMTOP As Long = 10000000

        ' Try sequential for
        sw = Stopwatch.StartNew()
        For i As Long = 0 To SUMTOP - 1
            sum += i
        Next
        sw.Stop()
        Console.WriteLine("sequential for result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds)

        ' Try parallel for with locals
        sum = 0
        sw = Stopwatch.StartNew()
        Parallel.For(0L, SUMTOP, Function() 0L, Function(item, state, prevLocal) prevLocal + item, Function(local) Interlocked.Add(sum, local))
        sw.Stop()
        Console.WriteLine("parallel for w/locals result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds)

        ' Try range partitioner
        sum = 0
        sw = Stopwatch.StartNew()
        Parallel.ForEach(Partitioner.Create(0L, SUMTOP),
                         Sub(range)
                             Dim local As Long = 0
                             For i As Long = range.Item1 To range.Item2 - 1
                                 local += i
                             Next
                             Interlocked.Add(sum, local)
                         End Sub)
        sw.Stop()
        Console.WriteLine("range partitioner result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds)
    End Sub

End Module
開發者ID:VB.NET開發者,項目名稱:System.Threading.Tasks,代碼行數:44,代碼來源:Parallel.ForEach


注:本文中的System.Threading.Tasks.Parallel.ForEach方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。