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


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

本文整理匯總了VB.NET中System.Collections.Concurrent.ConcurrentStack<T>的典型用法代碼示例。如果您正苦於以下問題:VB.NET ConcurrentStack<T>類的具體用法?VB.NET ConcurrentStack<T>怎麽用?VB.NET ConcurrentStack<T>使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


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

示例1: Example

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

Class Example
    ' Demonstrates:
    '   ConcurrentStack<T>.Push();
    '   ConcurrentStack<T>.TryPeek();
    '   ConcurrentStack<T>.TryPop();
    '   ConcurrentStack<T>.Clear();
    '   ConcurrentStack<T>.IsEmpty;
    Shared Sub Main()
        Dim items As Integer = 10000
        Dim stack As ConcurrentStack(Of Integer) = New ConcurrentStack(Of Integer)()

        ' Create an action to push items onto the stack
        Dim pusher As Action = Function()
                                   For i As Integer = 0 To items - 1
                                       stack.Push(i)
                                   Next
                               End Function

        ' Run the action once
        pusher()

        Dim result As Integer = Nothing

        If stack.TryPeek(result) Then
            Console.WriteLine($"TryPeek() saw {result} on top of the stack.")
        Else
            Console.WriteLine("Could not peek most recently added number.")
        End If

        ' Empty the stack
        stack.Clear()

        If stack.IsEmpty Then
            Console.WriteLine("Cleared the stack.")
        End If

        ' Create an action to push and pop items
        Dim pushAndPop As Action = Function()
                                       Console.WriteLine($"Task started on {Task.CurrentId}")
                                       Dim item As Integer

                                       For i As Integer = 0 To items - 1
                                           stack.Push(i)
                                       Next

                                       For i As Integer = 0 To items - 1
                                           stack.TryPop(item)
                                       Next

                                       Console.WriteLine($"Task ended on {Task.CurrentId}")
                                   End Function

        ' Spin up five concurrent tasks of the action
        Dim tasks = New Task(4) {}
        For i As Integer = 0 To tasks.Length - 1
            tasks(i) = Task.Factory.StartNew(pushAndPop)
        Next

        ' Wait for all the tasks to finish up
        Task.WaitAll(tasks)

        If Not stack.IsEmpty Then
            Console.WriteLine("Did not take all the items off the stack")
        End If
    End Sub
End Class
開發者ID:VB.NET開發者,項目名稱:System.Collections.Concurrent,代碼行數:70,代碼來源:ConcurrentStack

示例2: Example

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

Class Example
    ' Demonstrates:
    '   ConcurrentStack<T>.PushRange();
    '   ConcurrentStack<T>.TryPopRange();
    Shared Sub Main()
        Dim numParallelTasks As Integer = 4
        Dim numItems As Integer = 1000
        Dim stack = New ConcurrentStack(Of Integer)()

        ' Push a range of values onto the stack concurrently
        Task.WaitAll(Enumerable.Range(0, numParallelTasks).[Select](
                     Function(i) Task.Factory.StartNew(
                        Function(state)
                            Dim index As Integer = CInt(state)
                            Dim array As Integer() = New Integer(numItems - 1) {}

                            For j As Integer = 0 To numItems - 1
                                array(j) = index + j
                            Next

                            Console.WriteLine($"Pushing an array of ints from {array(0)} to {array(numItems - 1)}")
                            stack.PushRange(array)
                        End Function, i * numItems, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.[Default])).ToArray())


        Dim numTotalElements As Integer = 4 * numItems
        Dim resultBuffer As Integer() = New Integer(numTotalElements - 1) {}
        Task.WaitAll(Enumerable.Range(0, numParallelTasks).[Select](
                     Function(i) Task.Factory.StartNew(
                        Function(obj)
                            Dim index As Integer = CInt(obj)
                            Dim result As Integer = stack.TryPopRange(resultBuffer, index, numItems)
                            Console.WriteLine($"TryPopRange expected {numItems}, got {result}.")
                        End Function, i * numItems, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.[Default])).ToArray())

        For i As Integer = 0 To numParallelTasks - 1
            ' Create a sequence we expect to see from the stack taking the last number of the range we inserted
            Dim expected = Enumerable.Range(resultBuffer(i * numItems + numItems - 1), numItems)

            ' Take the range we inserted, reverse it, and compare to the expected sequence
            Dim areEqual = expected.SequenceEqual(resultBuffer.Skip(i * numItems).Take(numItems).Reverse())

            If areEqual Then
                Console.WriteLine($"Expected a range of {expected.First()} to {expected.Last()}. Got {resultBuffer(i * numItems + numItems - 1)} to {resultBuffer(i * numItems)}")
            Else
                Console.WriteLine($"Unexpected consecutive ranges.")
            End If
        Next
    End Sub
End Class
開發者ID:VB.NET開發者,項目名稱:System.Collections.Concurrent,代碼行數:56,代碼來源:ConcurrentStack


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