Producer and Consumer with Unsynchronized Integer Buffer : Producer Consumer « Thread « VB.Net






Producer and Consumer with Unsynchronized Integer Buffer

Producer and Consumer with Unsynchronized Integer Buffer
Imports System
Imports System.Threading

Public Class MainClass

   Shared Sub Main()

      ' create shared object used by threads
      Dim holdInteger As New UnsynchronizedIntegerBuffer()

      ' Random object used by each thread
      Dim randomObject As New Random()

      ' create Producer and Consumer objects
      Dim producer As New CProducer(holdInteger, randomObject)
      Dim consumer As New CConsumer(holdInteger, randomObject)

      ' create threads for producer and consumer  
      ' set delegates for each thread
      Dim producerThread As New Thread(AddressOf producer.Produce)
      Dim consumerThread As New Thread(AddressOf consumer.Consume)

      ' name each thread
      producerThread.Name = "Producer"
      consumerThread.Name = "Consumer"

      ' start each thread
      producerThread.Start()
      consumerThread.Start()

   End Sub ' Main

End Class

' Produces integers from 1 to 4 and places them in unsynchronized buffer.

Public Class CProducer
   Private sharedLocation As UnsynchronizedIntegerBuffer
   Private randomSleepTime As Random

   Public Sub New(ByVal sharedObject As _
      UnsynchronizedIntegerBuffer, ByVal randomObject As Random)

      sharedLocation = sharedObject
      randomSleepTime = randomObject
   End Sub

   Public Sub Produce()
      Dim count As Integer
      For count = 1 To 4
         Thread.Sleep(randomSleepTime.Next(3000))
         sharedLocation.Buffer = count
      Next

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

End Class

' Consumes 4 integers from unsynchronized buffer.
Public Class CConsumer
   Private sharedLocation As UnsynchronizedIntegerBuffer
   Private randomSleepTime As Random

   Public Sub New(ByVal sharedObject As _
      UnsynchronizedIntegerBuffer, ByVal randomObject As Random)

      sharedLocation = sharedObject
      randomSleepTime = randomObject
   End Sub ' New

   Public Sub Consume()
      Dim count, sum As Integer

      For count = 1 To 4
         Thread.Sleep(randomSleepTime.Next(3000))
         sum += sharedLocation.Buffer
      Next

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

End Class

' Definition of a shared integer without synchronization mechanisms.

Public Class UnsynchronizedIntegerBuffer

   Private mBuffer As Integer = -1

   Property Buffer() As Integer

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

         Return mBuffer
      End Get

      Set(ByVal Value As Integer)
         Console.WriteLine(Thread.CurrentThread.Name & _
            " writes " & Value)

         mBuffer = Value
      End Set

   End Property ' Buffer

End Class
           
       








Related examples in the same category

1.Thread Producer and ConsumerThread Producer and Consumer
2.Producer and Consumer with Synchronized Integer BufferProducer and Consumer with Synchronized Integer Buffer