本文整理汇总了VB.NET中System.Threading.Interlocked.Decrement方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Interlocked.Decrement方法的具体用法?VB.NET Interlocked.Decrement怎么用?VB.NET Interlocked.Decrement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Interlocked 的用法示例。
在下文中一共展示了Interlocked.Decrement方法的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 = 10000
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.Decrement(midpointCount)
midpt += 1
End If
total += 1
Loop While midpointCount > 0
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.Decrement 输出:
Thread Thread2:
Random Numbers: 3,204,021
Midpoint values: 3,156 (0.099 %)
Thread Thread0:
Random Numbers: 4,073,592
Midpoint values: 4,015 (0.099 %)
Thread Thread1:
Random Numbers: 2,828,192
Midpoint values: 2,829 (0.100 %)
Total midpoint values: 10,000 (0.099 %)
Total number of values: 10,105,805
示例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 = 50000
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.Decrement(midpointCount)
midpt += 1
End If
total += 1
Loop While midpointCount > 0
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.Decrement 输出:
Task 1:
Random Numbers: 24,530,624
Midpoint values: 24,675 (0.101 %)
Task 2:
Random Numbers: 7,079,718
Midpoint values: 7,093 (0.100 %)
Task 3:
Random Numbers: 18,284,617
Midpoint values: 18,232 (0.100 %)
Total midpoint values: 50,000 (0.100 %)
Total number of values: 49,894,959
注:本文中的System.Threading.Interlocked.Decrement方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论