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


VB.NET ConcurrentQueue<T>類代碼示例

本文整理匯總了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
開發者ID:VB.NET開發者,項目名稱:System.Collections.Concurrent,代碼行數:46,代碼來源:ConcurrentQueue


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