Producer and consumer without 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.2.Producer and consumer without 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

   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
Consumer reads -1
Consumer reads -1
Consumer reads -1
Consumer reads -1
Consumer read values totaling: -4.
Terminating Consumer.
Producer writes 1
Producer writes 2
Producer writes 3
^CTerminate batch job (Y/N)? ^Cn
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.