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.Development
8.Event
9.File Directory
10.Generics
11.GUI
12.Language Basics
13.LINQ
14.Network Remote
15.Security
16.Thread
17.Windows Presentation Foundation
18.Windows System
19.XML
20.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.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.