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

VB.Net
1. 2D
2. Application
3. Class
4. Data Structure
5. Database ADO.net
6. Development
7. Event
8. File Directory
9. Generics
10. GUI
11. Language Basics
12. Network Remote
13. Thread
14. Windows System
15. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net Tutorial
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
w___w_w_.j_a__v___a___2s___._com | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.