VB.Net/Thread/Thread Synchronize — различия между версиями

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

Текущая версия на 12:45, 26 мая 2010

Synchronize Thread Demo

Imports System
Imports System.Threading
Imports System.Text
Imports System.Windows.Forms
Public Class MainClass
  
  Public Shared Sub Main()
    Dim myHouse As New House()
  End Sub
  
End Class

Public Class House
  Dim house As String = New String("Lock")
  Public Sub New()
    Dim aThreadStart As Threading.ThreadStart
    Dim aThread As Thread
    Dim i As Integer
    For i = 0 To 10
      aThreadStart = New ThreadStart(AddressOf ChangeTemperature)
      aThread = New Thread(aThreadStart)
      aThread.Name = CStr(i) & ""th room"
      aThread.Start()
    Next
  End Sub
  Private Sub ChangeTemperature()
    SyncLock (house)
       Console.WriteLine("Checking 1")
       Console.WriteLine("Checking 2")
       Console.WriteLine("Checking 3")
       Try
          Thread.Sleep(1000)
       Catch E As ThreadInterruptedException
          "thread ended 
       End Try
       
       Console.WriteLine("Checking 4")
       Console.WriteLine("Checking 5")
       Console.WriteLine("Checking 6")
       Console.WriteLine("Checking 7")
       Console.WriteLine("Checking 8")
       Console.WriteLine("Checking 9")
       Console.WriteLine("Checking 10")
    End SyncLock
  End Sub
End Class


Thread without Synchronizing

Imports System
Imports System.Threading
Imports System.Text
Imports System.Windows.Forms
Public Class MainClass
  
  Public Shared Sub Main()
    Dim myHouse As New House()
  End Sub
  
End Class

Public Class House
  Dim house As String = New String("Lock")
  Public Sub New()
    Dim aThreadStart As Threading.ThreadStart
    Dim aThread As Thread
    Dim i As Integer
    For i = 0 To 10
      aThreadStart = New ThreadStart(AddressOf ChangeTemperature)
      aThread = New Thread(aThreadStart)
      aThread.Name = CStr(i) & ""th room"
      aThread.Start()
    Next
  End Sub
  Private Sub ChangeTemperature()
 "   SyncLock (house)
       Console.WriteLine("Checking 1")
       Console.WriteLine("Checking 2")
       Console.WriteLine("Checking 3")
       Try
          Thread.Sleep(1000)
       Catch E As ThreadInterruptedException
          "thread ended 
       End Try
       
       Console.WriteLine("Checking 4")
       Console.WriteLine("Checking 5")
       Console.WriteLine("Checking 6")
       Console.WriteLine("Checking 7")
       Console.WriteLine("Checking 8")
       Console.WriteLine("Checking 9")
       Console.WriteLine("Checking 10")
"    End SyncLock
  End Sub
End Class


Two threads without SyncLock

Imports System
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Imports System.IO

public class MainClass
   Shared counter As Integer
   Shared locker As Object = New Object()
   Shared Sub Main()
        Dim aThread As Thread = New Thread(AddressOf Calculate)
        Dim bThread As Thread = New Thread(AddressOf Calculate)
        aThread.Start()
        bThread.Start()
   End Sub

   Shared Sub Calculate()
        Console.WriteLine("Thread " & Thread.CurrentThread.GetHashCode & " woke up. Counter=" & counter)
"        SyncLock locker
           Console.WriteLine("In Locker")
           Console.WriteLine("1")
           Console.WriteLine("2")
           
           Dim i As Integer = 0
           Dim j As Integer = 0
           
           For i = 0 To 10000
               For j = 0  To 10000
                   
               Next j
           Next i
           Console.WriteLine("3")
           Console.WriteLine("4")
           Console.WriteLine("5")
 "       End SyncLock
        Console.WriteLine("Out Locker")
        Console.WriteLine("Thread " & Thread.CurrentThread.GetHashCode & " woke up. Counter=" & counter)
    End Sub
End Class


Two threads with SyncLock

Imports System
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Imports System.IO

public class MainClass
   Shared counter As Integer
   Shared locker As Object = New Object()
   Shared Sub Main()
        Dim aThread As Thread = New Thread(AddressOf Calculate)
        Dim bThread As Thread = New Thread(AddressOf Calculate)
        aThread.Start()
        bThread.Start()
   End Sub

   Shared Sub Calculate()
        Console.WriteLine("Thread " & Thread.CurrentThread.GetHashCode & " woke up. Counter=" & counter)
        SyncLock locker
           Console.WriteLine("In Locker")
           Console.WriteLine("1")
           Console.WriteLine("2")
           
           Dim i As Integer = 0
           Dim j As Integer = 0
           
           For i = 0 To 10000
               For j = 0  To 10000
                   
               Next j
           Next i
           Console.WriteLine("3")
           Console.WriteLine("4")
           Console.WriteLine("5")
        End SyncLock
        Console.WriteLine("Out Locker")
        Console.WriteLine("Thread " & Thread.CurrentThread.GetHashCode & " woke up. Counter=" & counter)
    End Sub
End Class