/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrThreading/VB/class2.vb
64 строки
2 KB
Andy De George
Move snippets from samples repo to this repo (#17296)
03 мар 2020, 19:23
Не верифицирован
03 мар 2020, 19:23
dbbeda1
Код
Авторство
О чём код?
' SyncLock Statement ' 14501703298f4d43b139c4b6366af176 ' <snippet21> Imports System.Threading Module Module1 Class Account Dim thisLock As New Object Dim balance As Integer Dim r As New Random() Public Sub New(ByVal initial As Integer) balance = initial End Sub Public Function Withdraw(ByVal amount As Integer) As Integer ' This condition will never be true unless the SyncLock statement ' is commented out: If balance < 0 Then Throw New Exception("Negative Balance") End If ' Comment out the SyncLock and End SyncLock lines to see ' the effect of leaving out the SyncLock keyword. SyncLock thisLock If balance >= amount Then Console.WriteLine("Balance before Withdrawal : " & balance) Console.WriteLine("Amount to Withdraw : -" & amount) balance = balance - amount Console.WriteLine("Balance after Withdrawal : " & balance) Return amount Else ' Transaction rejected. Return 0 End If End SyncLock End Function Public Sub DoTransactions() For i As Integer = 0 To 99 Withdraw(r.Next(1, 100)) Next End Sub End Class Sub Main() Dim threads(10) As Thread Dim acc As New Account(1000) For i As Integer = 0 To 9 Dim t As New Thread(New ThreadStart(AddressOf acc.DoTransactions)) threads(i) = t Next For i As Integer = 0 To 9 threads(i).Start() Next End Sub End Module ' </snippet21>