KD̉𓚁B4VB.NETŁiVB04.vbj

Imports System
Imports System.Threading

Public Class Student
    Private Name As String
    Private Count As Integer

    Public Sub New(ByVal Name As String)
        Me.Name = Name
        Count = 0
    End Sub

    Public Sub Study()
        SyncLock Me
            While Count < 10
                Count = Count + 1
                Console.WriteLine("{0} is studyng: {1}", Name, Count)
                Thread.Sleep(500)
            End While
        End SyncLock
    End Sub

    Public Sub Play()
        SyncLock Me
            While Count < 10
                Count = Count + 1
                Console.WriteLine("{0} is playing: {1}", Name, Count)
                Thread.Sleep(1000)
            End While
        End SyncLock
    End Sub
End Class

Module Module1

    Sub Main()
        Dim Tom As Student = New Student("Tom")
        Dim StudyThread As Thread = New Thread(New ThreadStart(AddressOf Tom.Study))
        Dim PlayThread As Thread = New Thread(New ThreadStart(AddressOf Tom.Play))
        StudyThread.Start()
        PlayThread.Start()
    End Sub

End Module