<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.vbex.ru/index.php?action=history&amp;feed=atom&amp;title=VB.Net%2FThread%2FProducer_Consumer</id>
		<title>VB.Net/Thread/Producer Consumer - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.vbex.ru/index.php?action=history&amp;feed=atom&amp;title=VB.Net%2FThread%2FProducer_Consumer"/>
		<link rel="alternate" type="text/html" href="http://www.vbex.ru/index.php?title=VB.Net/Thread/Producer_Consumer&amp;action=history"/>
		<updated>2026-04-05T12:26:09Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.vbex.ru/index.php?title=VB.Net/Thread/Producer_Consumer&amp;diff=912&amp;oldid=prev</id>
		<title> в 16:40, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.vbex.ru/index.php?title=VB.Net/Thread/Producer_Consumer&amp;diff=912&amp;oldid=prev"/>
				<updated>2010-05-26T16:40:06Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 16:40, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.vbex.ru/index.php?title=VB.Net/Thread/Producer_Consumer&amp;diff=913&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.vbex.ru/index.php?title=VB.Net/Thread/Producer_Consumer&amp;diff=913&amp;oldid=prev"/>
				<updated>2010-05-26T12:45:30Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Producer and Consumer with Synchronized Integer Buffer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;vbnet&amp;quot;&amp;gt;&lt;br /&gt;
&amp;quot; *************************************************************&lt;br /&gt;
&amp;quot; * (C) Copyright 2003 by Deitel &amp;amp; Associates, Inc.           *&lt;br /&gt;
&amp;quot; *     and Prentice Hall.                                    *&lt;br /&gt;
&amp;quot; * All Rights Reserved.                                      *&lt;br /&gt;
&amp;quot; *                                                           *&lt;br /&gt;
&amp;quot; * DISCLAIMER: The authors and publisher of this book have   *&lt;br /&gt;
&amp;quot; * used their best efforts in preparing the book. These      *&lt;br /&gt;
&amp;quot; * efforts include the development, research, and testing of *&lt;br /&gt;
&amp;quot; * the theories and programs to determine their              *&lt;br /&gt;
&amp;quot; * effectiveness. The authors and publisher make no warranty *&lt;br /&gt;
&amp;quot; * of any kind, expressed or implied, with regard to these   *&lt;br /&gt;
&amp;quot; * programs or to the documentation contained in these books.*&lt;br /&gt;
&amp;quot; * The authors and publisher shall not be liable in any event*&lt;br /&gt;
&amp;quot; * for incidental or consequential damages in connection     *&lt;br /&gt;
&amp;quot; * with, or arising out of, the furnishing, performance, or  *&lt;br /&gt;
&amp;quot; * use of these programs.                                    *&lt;br /&gt;
&amp;quot; *************************************************************&lt;br /&gt;
&lt;br /&gt;
Imports System&lt;br /&gt;
Imports System.Threading&lt;br /&gt;
Public Class MainClass&lt;br /&gt;
   Shared Sub Main()&lt;br /&gt;
      &amp;quot; create shared object used by threads&lt;br /&gt;
      Dim holdInteger As New SynchronizedIntegerBuffer()&lt;br /&gt;
      &amp;quot; Random object used by each thread&lt;br /&gt;
      Dim randomObject As New Random()&lt;br /&gt;
      &amp;quot; create Producer and Consumer objects&lt;br /&gt;
      Dim producer As New CProducer(holdInteger, randomObject)&lt;br /&gt;
      Dim consumer As New CConsumer(holdInteger, randomObject)&lt;br /&gt;
      &amp;quot; create threads for producer and consumer  &lt;br /&gt;
      &amp;quot; set delegates for each thread&lt;br /&gt;
      Dim producerThread As New Thread(AddressOf producer.Produce)&lt;br /&gt;
      Dim consumerThread As New Thread(AddressOf consumer.Consume)&lt;br /&gt;
      &amp;quot; name each thread&lt;br /&gt;
      producerThread.Name = &amp;quot;Producer&amp;quot;&lt;br /&gt;
      consumerThread.Name = &amp;quot;Consumer&amp;quot;&lt;br /&gt;
      &amp;quot; start each thread&lt;br /&gt;
      producerThread.Start()&lt;br /&gt;
      consumerThread.Start()&lt;br /&gt;
   End Sub &amp;quot; Main&lt;br /&gt;
End Class&lt;br /&gt;
&amp;quot; Produces integers from 1 to 4 and places them in unsynchronized buffer.&lt;br /&gt;
Public Class CProducer&lt;br /&gt;
   Private sharedLocation As SynchronizedIntegerBuffer&lt;br /&gt;
   Private randomSleepTime As Random&lt;br /&gt;
   Public Sub New(ByVal sharedObject As _&lt;br /&gt;
      SynchronizedIntegerBuffer, ByVal randomObject As Random)&lt;br /&gt;
      sharedLocation = sharedObject&lt;br /&gt;
      randomSleepTime = randomObject&lt;br /&gt;
   End Sub&lt;br /&gt;
   Public Sub Produce()&lt;br /&gt;
      Dim count As Integer&lt;br /&gt;
      For count = 1 To 4&lt;br /&gt;
         Thread.Sleep(randomSleepTime.Next(3000))&lt;br /&gt;
         sharedLocation.Buffer = count&lt;br /&gt;
      Next&lt;br /&gt;
      Console.WriteLine(Thread.CurrentThread.Name &amp;amp; _&lt;br /&gt;
         &amp;quot; done producing.&amp;quot; &amp;amp; vbCrLf &amp;amp; &amp;quot;Terminating &amp;quot; &amp;amp; _&lt;br /&gt;
         Thread.CurrentThread.Name &amp;amp; &amp;quot;.&amp;quot;)&lt;br /&gt;
   End Sub&lt;br /&gt;
End Class&lt;br /&gt;
&amp;quot; Consumes 4 integers from unsynchronized buffer.&lt;br /&gt;
Public Class CConsumer&lt;br /&gt;
   Private sharedLocation As SynchronizedIntegerBuffer&lt;br /&gt;
   Private randomSleepTime As Random&lt;br /&gt;
   Public Sub New(ByVal sharedObject As _&lt;br /&gt;
      SynchronizedIntegerBuffer, ByVal randomObject As Random)&lt;br /&gt;
      sharedLocation = sharedObject&lt;br /&gt;
      randomSleepTime = randomObject&lt;br /&gt;
   End Sub &amp;quot; New&lt;br /&gt;
   Public Sub Consume()&lt;br /&gt;
      Dim count, sum As Integer&lt;br /&gt;
      For count = 1 To 4&lt;br /&gt;
         Thread.Sleep(randomSleepTime.Next(3000))&lt;br /&gt;
         sum += sharedLocation.Buffer&lt;br /&gt;
      Next&lt;br /&gt;
      Console.WriteLine(Thread.CurrentThread.Name &amp;amp; _&lt;br /&gt;
         &amp;quot; read values totaling: &amp;quot; &amp;amp; sum &amp;amp; &amp;quot;.&amp;quot; &amp;amp; vbCrLf &amp;amp; _&lt;br /&gt;
         &amp;quot;Terminating &amp;quot; &amp;amp; Thread.CurrentThread.Name &amp;amp; &amp;quot;.&amp;quot;)&lt;br /&gt;
   End Sub &amp;quot; Consume&lt;br /&gt;
End Class&lt;br /&gt;
&amp;quot; Synchronizes access to an Integer.&lt;br /&gt;
Public Class SynchronizedIntegerBuffer&lt;br /&gt;
   Private mBuffer As Integer = -1&lt;br /&gt;
   Private occupiedBufferCount As Integer&lt;br /&gt;
   Public Property Buffer() As Integer&lt;br /&gt;
      Get&lt;br /&gt;
         &amp;quot; obtain lock on this object&lt;br /&gt;
         Monitor.Enter(Me)&lt;br /&gt;
         If occupiedBufferCount = 0 Then&lt;br /&gt;
            Console.WriteLine(Thread.CurrentThread.Name &amp;amp; _&lt;br /&gt;
               &amp;quot; tries to read.&amp;quot;)&lt;br /&gt;
            DisplayState(&amp;quot;Buffer empty. &amp;quot; &amp;amp; _&lt;br /&gt;
               Thread.CurrentThread.Name &amp;amp; &amp;quot; waits.&amp;quot;)&lt;br /&gt;
            Monitor.Wait(Me)&lt;br /&gt;
         End If&lt;br /&gt;
         occupiedBufferCount -= 1&lt;br /&gt;
         DisplayState(Thread.CurrentThread.Name &amp;amp; &amp;quot; reads &amp;quot; &amp;amp; _&lt;br /&gt;
            mBuffer)&lt;br /&gt;
         Monitor.Pulse(Me)&lt;br /&gt;
         &amp;quot; Get copy of buffer before releasing lock.&lt;br /&gt;
         &amp;quot; It is possible that the producer could be&lt;br /&gt;
         &amp;quot; assigned the processor immediately after the &lt;br /&gt;
         &amp;quot; monitor is released and before the return&lt;br /&gt;
         &amp;quot; statement executes. In this case, the producer&lt;br /&gt;
         &amp;quot; would assign a new value to buffer before the&lt;br /&gt;
         &amp;quot; return statement returns the value to the &lt;br /&gt;
         &amp;quot; consumer. Thus, the consumer would receive the&lt;br /&gt;
         &amp;quot; new value. Making a copy of buffer and &lt;br /&gt;
         &amp;quot; returning the copy helps ensure that the&lt;br /&gt;
         &amp;quot; consumer receives the proper value.&lt;br /&gt;
         Dim bufferCopy As Integer = mBuffer&lt;br /&gt;
         &amp;quot; release lock on this object&lt;br /&gt;
         Monitor.Exit(Me)&lt;br /&gt;
         Return bufferCopy&lt;br /&gt;
      End Get&lt;br /&gt;
      Set(ByVal Value As Integer)&lt;br /&gt;
         &amp;quot; acquire lock for this object&lt;br /&gt;
         Monitor.Enter(Me)&lt;br /&gt;
         &amp;quot; if there are no empty locations, place invoking&lt;br /&gt;
         &amp;quot; thread in WaitSleepJoin state&lt;br /&gt;
         If occupiedBufferCount = 1 Then&lt;br /&gt;
            Console.WriteLine(Thread.CurrentThread.Name &amp;amp; _&lt;br /&gt;
               &amp;quot; tries to write.&amp;quot;)&lt;br /&gt;
            DisplayState(&amp;quot;Buffer full. &amp;quot; &amp;amp; _&lt;br /&gt;
               Thread.CurrentThread.Name &amp;amp; &amp;quot; waits.&amp;quot;)&lt;br /&gt;
            Monitor.Wait(Me)&lt;br /&gt;
         End If&lt;br /&gt;
         &amp;quot; set new buffer value&lt;br /&gt;
         mBuffer = Value&lt;br /&gt;
         occupiedBufferCount += 1&lt;br /&gt;
         DisplayState(Thread.CurrentThread.Name &amp;amp; &amp;quot; writes &amp;quot; &amp;amp; _&lt;br /&gt;
            mBuffer)&lt;br /&gt;
         Monitor.Pulse(Me)&lt;br /&gt;
         Monitor.Exit(Me)&lt;br /&gt;
      End Set&lt;br /&gt;
   End Property&lt;br /&gt;
   Public Sub DisplayState(ByVal operation As String)&lt;br /&gt;
      Console.WriteLine(&amp;quot;{0,-35}{1,-9}{2}&amp;quot; &amp;amp; vbCrLf, _&lt;br /&gt;
         operation, mBuffer, occupiedBufferCount)&lt;br /&gt;
   End Sub&lt;br /&gt;
End Class&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Producer and Consumer with Unsynchronized Integer Buffer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;vbnet&amp;quot;&amp;gt;&lt;br /&gt;
Imports System&lt;br /&gt;
Imports System.Threading&lt;br /&gt;
Public Class MainClass&lt;br /&gt;
   Shared Sub Main()&lt;br /&gt;
      &amp;quot; create shared object used by threads&lt;br /&gt;
      Dim holdInteger As New UnsynchronizedIntegerBuffer()&lt;br /&gt;
      &amp;quot; Random object used by each thread&lt;br /&gt;
      Dim randomObject As New Random()&lt;br /&gt;
      &amp;quot; create Producer and Consumer objects&lt;br /&gt;
      Dim producer As New CProducer(holdInteger, randomObject)&lt;br /&gt;
      Dim consumer As New CConsumer(holdInteger, randomObject)&lt;br /&gt;
      &amp;quot; create threads for producer and consumer  &lt;br /&gt;
      &amp;quot; set delegates for each thread&lt;br /&gt;
      Dim producerThread As New Thread(AddressOf producer.Produce)&lt;br /&gt;
      Dim consumerThread As New Thread(AddressOf consumer.Consume)&lt;br /&gt;
      &amp;quot; name each thread&lt;br /&gt;
      producerThread.Name = &amp;quot;Producer&amp;quot;&lt;br /&gt;
      consumerThread.Name = &amp;quot;Consumer&amp;quot;&lt;br /&gt;
      &amp;quot; start each thread&lt;br /&gt;
      producerThread.Start()&lt;br /&gt;
      consumerThread.Start()&lt;br /&gt;
   End Sub &amp;quot; Main&lt;br /&gt;
End Class&lt;br /&gt;
&amp;quot; Produces integers from 1 to 4 and places them in unsynchronized buffer.&lt;br /&gt;
Public Class CProducer&lt;br /&gt;
   Private sharedLocation As UnsynchronizedIntegerBuffer&lt;br /&gt;
   Private randomSleepTime As Random&lt;br /&gt;
   Public Sub New(ByVal sharedObject As _&lt;br /&gt;
      UnsynchronizedIntegerBuffer, ByVal randomObject As Random)&lt;br /&gt;
      sharedLocation = sharedObject&lt;br /&gt;
      randomSleepTime = randomObject&lt;br /&gt;
   End Sub&lt;br /&gt;
   Public Sub Produce()&lt;br /&gt;
      Dim count As Integer&lt;br /&gt;
      For count = 1 To 4&lt;br /&gt;
         Thread.Sleep(randomSleepTime.Next(3000))&lt;br /&gt;
         sharedLocation.Buffer = count&lt;br /&gt;
      Next&lt;br /&gt;
      Console.WriteLine(Thread.CurrentThread.Name &amp;amp; _&lt;br /&gt;
         &amp;quot; done producing.&amp;quot; &amp;amp; vbCrLf &amp;amp; &amp;quot;Terminating &amp;quot; &amp;amp; _&lt;br /&gt;
         Thread.CurrentThread.Name &amp;amp; &amp;quot;.&amp;quot;)&lt;br /&gt;
   End Sub&lt;br /&gt;
End Class&lt;br /&gt;
&amp;quot; Consumes 4 integers from unsynchronized buffer.&lt;br /&gt;
Public Class CConsumer&lt;br /&gt;
   Private sharedLocation As UnsynchronizedIntegerBuffer&lt;br /&gt;
   Private randomSleepTime As Random&lt;br /&gt;
   Public Sub New(ByVal sharedObject As _&lt;br /&gt;
      UnsynchronizedIntegerBuffer, ByVal randomObject As Random)&lt;br /&gt;
      sharedLocation = sharedObject&lt;br /&gt;
      randomSleepTime = randomObject&lt;br /&gt;
   End Sub &amp;quot; New&lt;br /&gt;
   Public Sub Consume()&lt;br /&gt;
      Dim count, sum As Integer&lt;br /&gt;
      For count = 1 To 4&lt;br /&gt;
         Thread.Sleep(randomSleepTime.Next(3000))&lt;br /&gt;
         sum += sharedLocation.Buffer&lt;br /&gt;
      Next&lt;br /&gt;
      Console.WriteLine(Thread.CurrentThread.Name &amp;amp; _&lt;br /&gt;
         &amp;quot; read values totaling: &amp;quot; &amp;amp; sum &amp;amp; &amp;quot;.&amp;quot; &amp;amp; vbCrLf &amp;amp; _&lt;br /&gt;
         &amp;quot;Terminating &amp;quot; &amp;amp; Thread.CurrentThread.Name &amp;amp; &amp;quot;.&amp;quot;)&lt;br /&gt;
   End Sub &amp;quot; Consume&lt;br /&gt;
End Class&lt;br /&gt;
&amp;quot; Definition of a shared integer without synchronization mechanisms.&lt;br /&gt;
Public Class UnsynchronizedIntegerBuffer&lt;br /&gt;
   Private mBuffer As Integer = -1&lt;br /&gt;
   Property Buffer() As Integer&lt;br /&gt;
      Get&lt;br /&gt;
         Console.WriteLine(Thread.CurrentThread.Name &amp;amp; _&lt;br /&gt;
            &amp;quot; reads &amp;quot; &amp;amp; mBuffer)&lt;br /&gt;
         Return mBuffer&lt;br /&gt;
      End Get&lt;br /&gt;
      Set(ByVal Value As Integer)&lt;br /&gt;
         Console.WriteLine(Thread.CurrentThread.Name &amp;amp; _&lt;br /&gt;
            &amp;quot; writes &amp;quot; &amp;amp; Value)&lt;br /&gt;
         mBuffer = Value&lt;br /&gt;
      End Set&lt;br /&gt;
   End Property &amp;quot; Buffer&lt;br /&gt;
End Class&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Thread Producer and Consumer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;vbnet&amp;quot;&amp;gt;&lt;br /&gt;
Imports System.Threading&lt;br /&gt;
Module Module1&lt;br /&gt;
    Public Buffer As Integer&lt;br /&gt;
    Public BufferEmpty As Boolean = True&lt;br /&gt;
    Public MonitorLock As Object = New Object()&lt;br /&gt;
    Sub Producer()&lt;br /&gt;
        Dim Value As Integer = 0&lt;br /&gt;
        Do&lt;br /&gt;
            Monitor.Enter(MonitorLock)&lt;br /&gt;
            If (BufferEmpty) Then&lt;br /&gt;
                BufferEmpty = False&lt;br /&gt;
                Buffer = Value&lt;br /&gt;
                If (Value = 0) Then&lt;br /&gt;
                    Value = 1&lt;br /&gt;
                Else&lt;br /&gt;
                    Value = 0&lt;br /&gt;
                End If&lt;br /&gt;
                Console.WriteLine(&amp;quot;Producer: &amp;quot; &amp;amp; Buffer)&lt;br /&gt;
            End If&lt;br /&gt;
            Monitor.Exit(MonitorLock)&lt;br /&gt;
        Loop While (True)&lt;br /&gt;
    End Sub&lt;br /&gt;
    Sub Consumer()&lt;br /&gt;
        Dim Value As Integer&lt;br /&gt;
        Do&lt;br /&gt;
            Monitor.Enter(MonitorLock)&lt;br /&gt;
            If (Not BufferEmpty) Then&lt;br /&gt;
                BufferEmpty = True&lt;br /&gt;
                Thread.CurrentThread.Sleep(1000)&lt;br /&gt;
                Value = Buffer&lt;br /&gt;
                Console.WriteLine(&amp;quot;Consumer: &amp;quot; &amp;amp; Value)&lt;br /&gt;
            End If&lt;br /&gt;
            Monitor.Exit(MonitorLock)&lt;br /&gt;
        Loop While (True)&lt;br /&gt;
    End Sub&lt;br /&gt;
    Sub Main()&lt;br /&gt;
        Dim ProducerThread As Thread&lt;br /&gt;
        Dim ConsumerThread As Thread&lt;br /&gt;
        ProducerThread = New Thread(AddressOf Producer)&lt;br /&gt;
        ConsumerThread = New Thread(AddressOf Consumer)&lt;br /&gt;
        ProducerThread.Start()&lt;br /&gt;
        ConsumerThread.Start()&lt;br /&gt;
    End Sub&lt;br /&gt;
End Module&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>