Synchronize access to circular Integer buffer : 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.4.Synchronize access to circular Integer buffer
Imports System.Threading

Public Class Tester
   Public Shared Sub Main
      Dim buffer As New Buffer()
      Dim producer As New Producer(buffer)
      Dim consumer As New Consumer(buffer)
      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 Class 


Public Class Buffer
   Private mBuffer As Integer() {-1, -1, -1}

   Private occupiedBufferCount As Integer

   Private readlocation, writeLocation As Integer

   Property Buffer() As Integer
      Get
         SyncLock (Me)

            If occupiedBufferCount = Then
               Console.WriteLine("All buffers empty. " & _
                  Thread.CurrentThread.Name & " waits.")
               Monitor.Wait(Me)
            End If

            Dim readValue As Integer = mBuffer(readlocation)

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

            occupiedBufferCount -= 1

            readlocation = (readlocation + 1Mod mBuffer.Length

            Monitor.Pulse(Me)
            Return readValue
         End SyncLock

      End Get

      Set(ByVal Value As Integer)
         SyncLock (Me)
            If occupiedBufferCount = mBuffer.Length Then
               Console.WriteLine("All buffers full. " & _
                  Thread.CurrentThread.Name & " waits.")
               Monitor.Wait(Me)
            End If

            mBuffer(writeLocation= Value

            Console.WriteLine(Thread.CurrentThread.Name & " writes " & _
               mBuffer(writeLocation) )

            occupiedBufferCount += 1
            writeLocation = (writeLocation + 1Mod _
               mBuffer.Length

            Monitor.Pulse(Me)
         End SyncLock

      End Set

   End Property ' Buffer

End Class 


Public Class Producer
   Private buffer As Buffer

   Public Sub New(ByVal sharedObject As Buffer)
      buffer = sharedObject
   End Sub 
   Public Sub Produce()
      Dim count As Integer

      For count = To 10
         Thread.Sleep(1000)
         buffer.Buffer = count
      Next

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

End Class 

Public Class Consumer
   Private buffer As Buffer

   Public Sub New(ByVal sharedObject As Buffer)

      buffer = sharedObject
   End Sub 
   Public Sub Consume()
      Dim count, sum As Integer

      For count = To 10
         Thread.Sleep(500)
         sum += buffer.Buffer
      Next

      Console.WriteLine("Total " & _
         Thread.CurrentThread.Name & " consumed: " & sum & vbCrLf & _
         Thread.CurrentThread.Name & " terminated.")

   End Sub

End Class
All buffers empty. Consumer waits.
Producer writes 1
Consumer reads 1
All buffers empty. Consumer waits.
Producer writes 2
Consumer reads 2
All buffers empty. Consumer waits.
Producer writes 3
Consumer reads 3
All buffers empty. Consumer waits.
Producer writes 4
Consumer reads 4
All buffers empty. Consumer waits.
Producer writes 5
Consumer reads 5
All buffers empty. Consumer waits.
Producer writes 6
Consumer reads 6
All buffers empty. Consumer waits.
Producer writes 7
Consumer reads 7
All buffers empty. Consumer waits.
Producer writes 8
Consumer reads 8
All buffers empty. Consumer waits.
Producer writes 9
Consumer reads 9
All buffers empty. Consumer waits.
Producer writes 10
Producer done producing.
Producer terminated.
Consumer reads 10
Total Consumer consumed: 55
Consumer terminated.
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.