当前位置: 首页>>代码示例>>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;未经允许,请勿转载。