Producer and consumer with synchronization mechanisms : Producer and consumer « Thread « VB.Net Tutorial






Imports System.Threading

Module Tester
   Sub Main()
      Dim holdInteger As New Buffer()

      Dim producer As New Producer(holdInteger)
      Dim consumer As New Consumer(holdInteger)

      Dim producerThread As New Thread(AddressOf producer.Produce)
      Dim consumerThread As New Thread(AddressOf consumer.Consume)

      producerThread.Name = "Producer"
      consumerThread.Name = "Consumer"

      producerThread.Start()
      consumerThread.Start()
   End Sub

End Module 

Public Class Producer
   Private buffer As Buffer

   Public Sub New(ByVal sharedObject As Buffer)
      buffer = sharedObject
   End Sub ' New

   Public Sub Produce()
      Dim count As Integer

      For count = 1 To 4
         Thread.Sleep(4000)
         buffer.Buffer = count
      Next

      Console.WriteLine(Thread.CurrentThread.Name & _
         " done producing." & vbCrLf & "Terminating " & _
         Thread.CurrentThread.Name & ".")
   End Sub 

End Class 

Public Class Consumer
   Private buffer As Buffer

   Public Sub New(ByVal sharedObject As Buffer)
      buffer = sharedObject
   End Sub ' New

   Public Sub Consume()
      Dim count, sum As Integer

      For count = 1 To 4
         Thread.Sleep(300)
         sum += buffer.Buffer
      Next

      Console.WriteLine(Thread.CurrentThread.Name & _
         " read values totaling: " & sum & "." & vbCrLf & _
         "Terminating " & Thread.CurrentThread.Name & ".")
   End Sub 

End Class

Public Class Buffer
   Private mBuffer As Integer = -1
   Private occupiedBufferCount As Integer

   Public Property Buffer() As Integer
      Get
         Monitor.Enter(Me)
         If occupiedBufferCount = 0 Then
            Console.WriteLine("Buffer empty. " & _
               Thread.CurrentThread.Name & " waits.")

            Monitor.Wait(Me)
         End If

         occupiedBufferCount -= 1

         Console.WriteLine(Thread.CurrentThread.Name & " reads " & _
            mBuffer)

         Monitor.Pulse(Me)
         Dim bufferCopy As Integer = mBuffer

         Monitor.Exit(Me)

         Return bufferCopy
      End Get

      Set(ByVal Value As Integer)
         Monitor.Enter(Me)

         If occupiedBufferCount = 1 Then
            Console.WriteLine("Buffer full. " & _
               Thread.CurrentThread.Name & " waits.")

            Monitor.Wait(Me)
         End If

         mBuffer = Value

         occupiedBufferCount += 1

         Console.WriteLine(Thread.CurrentThread.Name & " writes " & _
            mBuffer)

         Monitor.Pulse(Me)
         Monitor.Exit(Me)
      End Set

   End Property ' Buffer

End Class
Buffer empty. Consumer waits.
Producer writes 1
Consumer reads 1
Buffer empty. Consumer waits.
Producer writes 2
Consumer reads 2
Buffer empty. Consumer waits.
Producer writes 3
Consumer reads 3
Buffer empty. Consumer waits.
Producer writes 4
Consumer reads 4
Consumer read values totaling: 10.
Terminating Consumer.
Producer done producing.
Terminating Producer.








23.6.Producer and consumer
23.6.1.Producer and consumer
23.6.2.Producer and consumer without synchronization mechanisms
23.6.3.Producer and consumer with synchronization mechanisms
23.6.4.Synchronize access to circular Integer buffer