VB.Net by API/System.Threading/Monitor

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

Monitor.Enter

  
Imports System.Threading
Public Class MainClass
   Shared Dim A As Thread = New Thread(AddressOf Method)
   Shared Dim B As Thread = New Thread(AddressOf Method)
   Shared Dim C As Thread = New Thread(AddressOf Method)

   Shared Public MonitorLock As Object = New Object()
   Public Shared Sub Main()
        A.Name = "A"
        A.Start()
        B.Name = "B"
        B.Start()
        C.Name = "C"
        C.Start()
   End Sub
   Shared Sub Method()
        Thread.CurrentThread.Sleep(Int(1000 * Rnd()))
        Monitor.Enter(MonitorLock)
        Console.WriteLine("Thread: " & Thread.CurrentThread.Name)
        Monitor.Exit(MonitorLock)
    End Sub
End Class


Monitor.Exit

  
Imports System.Threading
Public Class MainClass
   Shared Dim A As Thread = New Thread(AddressOf Method)
   Shared Dim B As Thread = New Thread(AddressOf Method)
   Shared Dim C As Thread = New Thread(AddressOf Method)

   Shared Public MonitorLock As Object = New Object()
   Public Shared Sub Main()
        A.Name = "A"
        A.Start()
        B.Name = "B"
        B.Start()
        C.Name = "C"
        C.Start()
   End Sub
   Shared Sub Method()
        Thread.CurrentThread.Sleep(Int(1000 * Rnd()))
        Monitor.Enter(MonitorLock)
        Console.WriteLine("Thread: " & Thread.CurrentThread.Name)
        Monitor.Exit(MonitorLock)
    End Sub
End Class