本文整理匯總了VB.NET中System.Collections.Concurrent.ConcurrentQueue<T>類的典型用法代碼示例。如果您正苦於以下問題:VB.NET ConcurrentQueue<T>類的具體用法?VB.NET ConcurrentQueue<T>怎麽用?VB.NET ConcurrentQueue<T>使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ConcurrentQueue<T>類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: TestQueue
' 導入命名空間
Imports System.Collections.Concurrent
Imports System.Threading
Imports System.Threading.Tasks
Class TestQueue
' Demonstrates:
' ConcurrentQueue<T>.Enqueue()
' ConcurrentQueue<T>.TryPeek()
' ConcurrentQueue<T>.TryDequeue()
Shared Sub Main()
' Construct a ConcurrentQueue
Dim cq As New ConcurrentQueue(Of Integer)()
' Populate the queue
For i As Integer = 0 To 9999
cq.Enqueue(i)
Next
' Peek at the first element
Dim result As Integer
If Not cq.TryPeek(result) Then
Console.WriteLine("CQ: TryPeek failed when it should have succeeded")
ElseIf result <> 0 Then
Console.WriteLine("CQ: Expected TryPeek result of 0, got {0}", result)
End If
Dim outerSum As Integer = 0
' An action to consume the ConcurrentQueue
Dim action As Action =
Sub()
Dim localSum As Integer = 0
Dim localValue As Integer
While cq.TryDequeue(localValue)
localSum += localValue
End While
Interlocked.Add(outerSum, localSum)
End Sub
' Start 4 concurrent consuming actions
Parallel.Invoke(action, action, action, action)
Console.WriteLine("outerSum = {0}, should be 49995000", outerSum)
End Sub
End Class