本文整理汇总了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
示例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
示例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