Circular Buffer Demo : Buffer « 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 » BufferScreenshots 
Circular Buffer Demo
Circular Buffer Demo

Imports System
Imports System.Threading

Public Class MainClass

   Shared Sub Main()
      ' create shared object
      Dim sharedLocation As New CHoldIntegerSynchronized()

      sharedLocation.CreateStateOutput()

      ' Random object used by each thread
      Dim randomObject As New Random()

      ' create CProducer and CConsumer objects
      Dim producer As New CProducer(sharedLocation, randomObject)

      Dim consumer As New CConsumer(sharedLocation, randomObject)

      ' create threads
      Dim producerThread As New Thread(AddressOf producer.Produce)

      Dim consumerThread As New Thread(AddressOf consumer.Consume)

      ' name threads
      producerThread.Name = "Producer"
      consumerThread.Name = "Consumer"

      ' start threads
      producerThread.Start()
      consumerThread.Start()

   End Sub ' Main

End Class

Public Class CConsumer
   Private sharedLocation As CHoldIntegerSynchronized
   Private randomSleepTime As Random

   ' constructor
   Public Sub New(ByVal sharedObject As CHoldIntegerSynchronized, _
      ByVal randomObject As Random)

      sharedLocation = sharedObject
      randomSleepTime = randomObject
   End Sub

   Public Sub Consume()
      Dim count, sum As Integer

      For count = To 10
         Thread.Sleep(randomSleepTime.Next(13000))
         sum += sharedLocation.Buffer
      Next

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

   End Sub

End Class

Public Class CHoldIntegerSynchronized

   Private mBuffer As Integer() {-1, -1, -1}

   Private occupiedBufferCount As Integer

   Private readlocation, writeLocation As Integer

   Public Sub New()
   End Sub ' New

   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.WriteLineThread.CurrentThread.Name & " reads " & _
               mBuffer(readlocation) )

            occupiedBufferCount -= 1

            readlocation = (readlocation + 1Mod mBuffer.Length

            CreateStateOutput() 

            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.WriteLineThread.CurrentThread.Name & " writes " & _
               mBuffer(writeLocation) )

            occupiedBufferCount += 1

            writeLocation = (writeLocation + 1Mod _
               mBuffer.Length

            CreateStateOutput() 
            Monitor.Pulse(Me)
         End SyncLock

      End Set

   End Property ' Buffer

   Public Sub CreateStateOutput()

      Dim As Integer
      Console.WriteLine"(buffers occupied: " & _
         occupiedBufferCount & ")" & vbCrLf & "buffers: " )

      For i = To mBuffer.GetUpperBound(0)
         Console.Write" " & mBuffer(i"  " )
      Next

      For i = To mBuffer.GetUpperBound(0)
         Console.Write"---- " )
      Next


      For i = To mBuffer.GetUpperBound(0)

         If (i = writeLocation AndAlso _
            writeLocation = readlocationThen

            Console.Write" WR  " )
         ElseIf i = writeLocation Then
            Console.Write" W   " )
         ElseIf i = readlocation Then
            Console.Write"  R  " )
         Else
            Console.Write"     " )
         End If

      Next
   End Sub

End Class

Public Class CProducer
   Private sharedLocation As CHoldIntegerSynchronized
   Private randomSleepTime As Random

   Public Sub New(ByVal sharedObject As CHoldIntegerSynchronized, _
      ByVal randomObject As Random)

      sharedLocation = sharedObject
      randomSleepTime = randomObject
   End Sub ' New

   Public Sub Produce()
      Dim count As Integer

      For count = 11 To 20
         Thread.Sleep(randomSleepTime.Next(13000))
         sharedLocation.Buffer = count
      Next

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

End Class
           
       
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.