当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET Interlocked.Increment方法代码示例

本文整理汇总了VB.NET中System.Threading.Interlocked.Increment方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Interlocked.Increment方法的具体用法?VB.NET Interlocked.Increment怎么用?VB.NET Interlocked.Increment使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Threading.Interlocked的用法示例。


在下文中一共展示了Interlocked.Increment方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: Example

' 导入命名空间
Imports System.Threading

Module Example
   Const LOWERBOUND As Integer = 0
   Const UPPERBOUND As Integer = 1001
   
   Dim lockObj As New Object()
   Dim rnd As New Random()
   Dim cte As CountdownEvent
   
   Dim totalCount As Integer = 0
   Dim totalMidpoint As Integer = 0
   Dim midpointCount As Integer = 0

   Public Sub Main()
      cte = New CountdownEvent(1)
      ' Start three threads. 
      For ctr As Integer = 0 To 2
         cte.AddCount()
         Dim th As New Thread(AddressOf GenerateNumbers)
         th.Name = "Thread" + ctr.ToString()
         th.Start()
      Next
      cte.Signal()
      cte.Wait()
      Console.WriteLine()
      Console.WriteLine("Total midpoint values:  {0,10:N0} ({1:P3})",
                        totalMidpoint, totalMidpoint/totalCount)
      Console.WriteLine("Total number of values: {0,10:N0}", 
                        totalCount)                  
   End Sub
   
   Private Sub GenerateNumbers()
      Dim midpoint As Integer = (upperBound - lowerBound) \ 2
      Dim value As Integer = 0
      Dim total As Integer = 0
      Dim midpt As Integer = 0
      Do
         SyncLock lockObj
            value = rnd.Next(lowerBound, upperBound)
         End SyncLock
         If value = midpoint Then 
            Interlocked.Increment(midpointCount)
            midpt += 1
         End If
         total += 1    
      Loop While midpointCount < 10000
      
      Interlocked.Add(totalCount, total)
      Interlocked.Add(totalMidpoint, midpt)
      
      Dim s As String = String.Format("Thread {0}:", Thread.CurrentThread.Name) + vbCrLf +
                        String.Format("   Random Numbers: {0:N0}", total) + vbCrLf +
                        String.Format("   Midpoint values: {0:N0} ({1:P3})", midpt, midpt/total)
      Console.WriteLine(s)
      cte.Signal()
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading,代码行数:59,代码来源:Interlocked.Increment

输出:

Thread Thread2:
Random Numbers: 2,776,674
Midpoint values: 2,773 (0.100 %)
Thread Thread1:
Random Numbers: 4,876,100
Midpoint values: 4,873 (0.100 %)
Thread Thread0:
Random Numbers: 2,312,310
Midpoint values: 2,354 (0.102 %)

Total midpoint values:      10,000 (0.100 %)
Total number of values:  9,965,084

示例2: Example

' 导入命名空间
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Const LOWERBOUND As Integer = 0
   Const UPPERBOUND As Integer = 1001
   
   Dim lockObj As New Object()
   Dim rnd As New Random()
   
   Dim totalCount As Integer = 0
   Dim totalMidpoint As Integer = 0
   Dim midpointCount As Integer = 0

   Public Sub Main()
      Dim tasks As New List(Of Task)()
      ' Start three tasks. 
      For ctr As Integer = 0 To 2
         tasks.Add(Task.Run( Sub()
                                Dim midpoint As Integer = (upperBound - lowerBound) \ 2
                                Dim value As Integer = 0
                                Dim total As Integer = 0
                                Dim midpt As Integer = 0
                                Do
                                   SyncLock lockObj
                                      value = rnd.Next(lowerBound, upperBound)
                                   End SyncLock
                                   If value = midpoint Then 
                                      Interlocked.Increment(midpointCount)
                                      midpt += 1
                                   End If
                                   total += 1    
                                Loop While midpointCount < 50000
                              
                                Interlocked.Add(totalCount, total)
                                Interlocked.Add(totalMidpoint, midpt)
                              
                                Dim s As String = String.Format("Task {0}:", Task.CurrentId) + vbCrLf +
                                                  String.Format("   Random Numbers: {0:N0}", total) + vbCrLf +
                                                  String.Format("   Midpoint values: {0:N0} ({1:P3})", midpt, midpt/total)
                                Console.WriteLine(s)
                             End Sub ))
      Next

      Task.WaitAll(tasks.ToArray())
      Console.WriteLine()
      Console.WriteLine("Total midpoint values:  {0,10:N0} ({1:P3})",
                        totalMidpoint, totalMidpoint/totalCount)
      Console.WriteLine("Total number of values: {0,10:N0}", 
                        totalCount)                  
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading,代码行数:54,代码来源:Interlocked.Increment

输出:

Task 3:
Random Numbers: 10,855,250
Midpoint values: 10,823 (0.100 %)
Task 1:
Random Numbers: 15,243,703
Midpoint values: 15,110 (0.099 %)
Task 2:
Random Numbers: 24,107,425
Midpoint values: 24,067 (0.100 %)

Total midpoint values:      50,000 (0.100 %)
Total number of values: 50,206,378


注:本文中的System.Threading.Interlocked.Increment方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。