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

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Thread » Producer and consumer 
23.6.3.Producer and consumer with synchronization mechanisms
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 = 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 = 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 = 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 = 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.