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

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Thread » Producer ConsumerScreenshots 
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 to 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 = 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 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 = 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
www_.___j__av___a__2___s___.___c_om__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.