VB.Net Tutorial/Thread/SyncLock

Материал из VB Эксперт
Перейти к: навигация, поиск

Producer and consumer with SyncLock

Imports System.Threading
Module Module1
    Public Buffer As Integer
    Public BufferEmpty As Boolean = True
    Public ProducerConsumerLock As Object = New Object()
    Sub Producer()
        Dim Value As Integer = 0
        Do
            SyncLock ProducerConsumerLock
                If (BufferEmpty) Then
                    BufferEmpty = False
                    Buffer = Value
                    If (Value = 0) Then
                        Value = 1
                    Else
                        Value = 0
                    End If
                    Console.WriteLine("Producer: " & Buffer)
                End If
            End SyncLock
        Loop While (True)
    End Sub
    Sub Consumer()
        Dim Value As Integer
        Do
            SyncLock ProducerConsumerLock
                If (Not BufferEmpty) Then
                    BufferEmpty = True
                    Thread.CurrentThread.Sleep(1000)
                    Value = Buffer
                    Console.WriteLine("Consumer: " & Value)
                End If
            End SyncLock
        Loop While (True)
    End Sub
    Sub Main()
        Dim ProducerThread As Thread = New Thread(AddressOf Producer)
        Dim ConsumerThread As Thread = New Thread(AddressOf Consumer)
        ProducerThread.Start()
        ConsumerThread.Start()
    End Sub
End Module
Producer: 0
Consumer: 0
Producer: 1
^CTerminate batch job (Y/N)? n

SyncLock

Imports System.Threading
public class Test
   Shared Dim obj As New someClass()
   public Shared Sub Main
               Dim thread1 As New Thread(AddressOf method1)
               Dim thread2 As New Thread(AddressOf method2)
               thread1.Start()
               thread2.Start()

   End Sub
       Private Shared Sub method1()
               SyncLock (obj)
                       Dim i As Integer
                       For i = 0 To 5
                               obj.someData += i
                               Console.WriteLine(obj.someData)
                       Next
               End SyncLock
       End Sub
       Private Shared Sub method2()
               SyncLock (obj)
                       Dim i As Integer
                       For i = 0 To 5
                               obj.someData += i
                               Console.WriteLine(obj.someData)
                       Next
               End SyncLock
       End Sub
   
End class

Public Class someClass
       Public Shared someData As Integer
End Class
0
1
3
6
10
15
15
16
18
21
25
30

Use SyncLock for shared resources

Imports System.Threading
Public Class Tester
    Shared WithEvents oSquare As SquareClass = New SquareClass()
    Public Shared Sub Main
        
        Dim t As Thread
        t = New Thread(AddressOf oSquare.CalcSquare)
        oSquare.Value = 3
        t.Start()
    
    End Sub
    Shared Sub SquareEventHandler(ByVal Square As Double) Handles oSquare.ThreadComplete
        Console.WriteLine(Square)
    End Sub
End Class
Public Class SquareClass
    Public Value As Double
    Public Square As Double
    Public Event ThreadComplete(ByVal Square As Double)
    Public Sub CalcSquare()
        Square = Value * Value
        RaiseEvent ThreadComplete(Square)
    End Sub
    Public Sub SyncCalcSquare()
        SyncLock GetType(SquareClass)
            Square = Value * Value
            MsgBox(Square)
        End SyncLock
    End Sub
End Class
9