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

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/C_Sharp/Thread/Producer_Consumer&amp;diff=1077&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Thread/Producer_Consumer&amp;diff=1077&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:19Z</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;Версия 15:31, 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://nfex.ru/index.php?title=Csharp/C_Sharp/Thread/Producer_Consumer&amp;diff=1078&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Thread/Producer_Consumer&amp;diff=1078&amp;oldid=prev"/>
				<updated>2010-05-26T11:42:57Z</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 comsumer in a synchronized buffer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
Code revised from Book published by &lt;br /&gt;
(C) Copyright 1992-2006 by Deitel &amp;amp; Associates, Inc. and&lt;br /&gt;
Pearson Education, Inc. All Rights Reserved.  &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
public class SynchronizedBuffer&lt;br /&gt;
{&lt;br /&gt;
   private int buffer = -1; &lt;br /&gt;
   private int occupiedBufferCount = 0;  &lt;br /&gt;
   public int Buffer&lt;br /&gt;
   {      &lt;br /&gt;
      get&lt;br /&gt;
      { &lt;br /&gt;
         Monitor.Enter( this );&lt;br /&gt;
         if ( occupiedBufferCount == 0 )&lt;br /&gt;
         {&lt;br /&gt;
            Console.WriteLine(Thread.CurrentThread.Name + &amp;quot; tries to read.&amp;quot; );&lt;br /&gt;
            DisplayState( &amp;quot;Buffer empty. &amp;quot; +Thread.CurrentThread.Name + &amp;quot; waits.&amp;quot; );&lt;br /&gt;
            Monitor.Wait( this );&lt;br /&gt;
         } &lt;br /&gt;
         --occupiedBufferCount;    &lt;br /&gt;
                              &lt;br /&gt;
         DisplayState( Thread.CurrentThread.Name + &amp;quot; reads &amp;quot; + buffer );&lt;br /&gt;
         Monitor.Pulse( this );&lt;br /&gt;
         int bufferCopy = buffer;&lt;br /&gt;
         Monitor.Exit( this );&lt;br /&gt;
         return bufferCopy;&lt;br /&gt;
      }&lt;br /&gt;
      set&lt;br /&gt;
      {&lt;br /&gt;
         Monitor.Enter( this );&lt;br /&gt;
         if ( occupiedBufferCount == 1 )&lt;br /&gt;
         {&lt;br /&gt;
            Console.WriteLine(Thread.CurrentThread.Name + &amp;quot; tries to write.&amp;quot; );&lt;br /&gt;
            DisplayState( &amp;quot;Buffer full. &amp;quot; + Thread.CurrentThread.Name + &amp;quot; waits.&amp;quot; );&lt;br /&gt;
            Monitor.Wait( this );&lt;br /&gt;
         }&lt;br /&gt;
         buffer = value;&lt;br /&gt;
         ++occupiedBufferCount;&lt;br /&gt;
         DisplayState( Thread.CurrentThread.Name + &amp;quot; writes &amp;quot; + buffer );&lt;br /&gt;
         Monitor.Pulse( this );&lt;br /&gt;
         Monitor.Exit( this );&lt;br /&gt;
      } &lt;br /&gt;
   }&lt;br /&gt;
   public void DisplayState( string operation )&lt;br /&gt;
   {&lt;br /&gt;
      Console.WriteLine( &amp;quot;{0,-35}{1,-9}{2}\n&amp;quot;,operation, buffer, occupiedBufferCount );&lt;br /&gt;
   }&lt;br /&gt;
   static void Main( string[] args )&lt;br /&gt;
   {&lt;br /&gt;
      SynchronizedBuffer shared = new SynchronizedBuffer();&lt;br /&gt;
      Random random = new Random();&lt;br /&gt;
      Console.WriteLine( &amp;quot;{0,-35}{1,-9}{2}\n&amp;quot;,&amp;quot;Operation&amp;quot;, &amp;quot;Buffer&amp;quot;, &amp;quot;Occupied Count&amp;quot; );&lt;br /&gt;
      shared.DisplayState( &amp;quot;Initial state&amp;quot; );&lt;br /&gt;
      Producer producer = new Producer( shared, random );&lt;br /&gt;
      Consumer consumer = new Consumer( shared, random );&lt;br /&gt;
      Thread producerThread = new Thread( new ThreadStart( producer.Produce ) );&lt;br /&gt;
      producerThread.Name = &amp;quot;Producer&amp;quot;;&lt;br /&gt;
      Thread consumerThread = new Thread( new ThreadStart( consumer.Consume ) );&lt;br /&gt;
      consumerThread.Name = &amp;quot;Consumer&amp;quot;;&lt;br /&gt;
      producerThread.Start();&lt;br /&gt;
      consumerThread.Start();&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
public class Consumer&lt;br /&gt;
{&lt;br /&gt;
   private SynchronizedBuffer sharedLocation;&lt;br /&gt;
   private Random randomSleepTime;&lt;br /&gt;
   public Consumer( SynchronizedBuffer shared, Random random )&lt;br /&gt;
   {&lt;br /&gt;
      sharedLocation = shared;&lt;br /&gt;
      randomSleepTime = random;&lt;br /&gt;
   }&lt;br /&gt;
   public void Consume()&lt;br /&gt;
   {&lt;br /&gt;
      int sum = 0;&lt;br /&gt;
      for ( int count = 1; count &amp;lt;= 10; count++ )&lt;br /&gt;
      {&lt;br /&gt;
         Thread.Sleep( randomSleepTime.Next( 1, 1001 ) );&lt;br /&gt;
         sum += sharedLocation.Buffer;&lt;br /&gt;
      }&lt;br /&gt;
      Console.WriteLine(&amp;quot;{0} read values totaling: {1}.\nTerminating {0}.&amp;quot;,Thread.CurrentThread.Name, sum );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
public class Producer &lt;br /&gt;
{&lt;br /&gt;
   private SynchronizedBuffer sharedLocation;&lt;br /&gt;
   private Random randomSleepTime;&lt;br /&gt;
   public Producer( SynchronizedBuffer shared, Random random )&lt;br /&gt;
   {&lt;br /&gt;
      sharedLocation = shared;&lt;br /&gt;
      randomSleepTime = random;&lt;br /&gt;
   }&lt;br /&gt;
   public void Produce()&lt;br /&gt;
   {&lt;br /&gt;
      for ( int count = 1; count &amp;lt;= 10; count++ ) &lt;br /&gt;
      {&lt;br /&gt;
         Thread.Sleep( randomSleepTime.Next( 1, 1001 ) );&lt;br /&gt;
         sharedLocation.Buffer = count; &lt;br /&gt;
      }&lt;br /&gt;
      Console.WriteLine( &amp;quot;{0} done producing.\nTerminating {0}.&amp;quot;,Thread.CurrentThread.Name );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Producer and consumer with a Circular Buffer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
Code revised from Book published by &lt;br /&gt;
(C) Copyright 1992-2006 by Deitel &amp;amp; Associates, Inc. and&lt;br /&gt;
Pearson Education, Inc. All Rights Reserved.  &lt;br /&gt;
*/&lt;br /&gt;
using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
public class Producer &lt;br /&gt;
{&lt;br /&gt;
   private CircularBuffer sharedLocation;&lt;br /&gt;
   private Random randomSleepTime;&lt;br /&gt;
   public Producer( CircularBuffer shared, Random random )&lt;br /&gt;
   {&lt;br /&gt;
      sharedLocation = shared;&lt;br /&gt;
      randomSleepTime = random;&lt;br /&gt;
   }&lt;br /&gt;
   public void Produce()&lt;br /&gt;
   {&lt;br /&gt;
      for ( int count = 1; count &amp;lt;= 10; count++ ) &lt;br /&gt;
      {&lt;br /&gt;
         Thread.Sleep( randomSleepTime.Next( 1, 3001 ) );&lt;br /&gt;
         sharedLocation.Buffer = count; &lt;br /&gt;
      } &lt;br /&gt;
      Console.WriteLine( &amp;quot;{0} done producing.\nTerminating {0}.&amp;quot;, Thread.CurrentThread.Name );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
public class Consumer&lt;br /&gt;
{&lt;br /&gt;
   private CircularBuffer sharedLocation;&lt;br /&gt;
   private Random randomSleepTime;&lt;br /&gt;
   public Consumer( CircularBuffer shared, Random random )&lt;br /&gt;
   {&lt;br /&gt;
      sharedLocation = shared;&lt;br /&gt;
      randomSleepTime = random;&lt;br /&gt;
   }&lt;br /&gt;
   public void Consume()&lt;br /&gt;
   {&lt;br /&gt;
      int sum = 0;&lt;br /&gt;
      for ( int count = 1; count &amp;lt;= 10; count++ )&lt;br /&gt;
      {&lt;br /&gt;
         Thread.Sleep( randomSleepTime.Next( 1, 3001 ) );&lt;br /&gt;
         sum += sharedLocation.Buffer;&lt;br /&gt;
      }&lt;br /&gt;
      Console.WriteLine(&amp;quot;{0} read values totaling: {1}.\nTerminating {0}.&amp;quot;,&lt;br /&gt;
         Thread.CurrentThread.Name, sum );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class CircularBuffer&lt;br /&gt;
{&lt;br /&gt;
   private int[] buffers = { -1, -1, -1 };&lt;br /&gt;
   private int occupiedBufferCount = 0;&lt;br /&gt;
   private int readLocation = 0; &lt;br /&gt;
   private int writeLocation = 0;&lt;br /&gt;
   &lt;br /&gt;
   public int Buffer&lt;br /&gt;
   {&lt;br /&gt;
      get&lt;br /&gt;
      {&lt;br /&gt;
         lock ( this )&lt;br /&gt;
         {&lt;br /&gt;
            if ( occupiedBufferCount == 0 )&lt;br /&gt;
            {&lt;br /&gt;
               Console.Write( &amp;quot;\nAll buffers empty. {0} waits.&amp;quot;,Thread.CurrentThread.Name );&lt;br /&gt;
               Monitor.Wait( this );&lt;br /&gt;
            } &lt;br /&gt;
            int readValue = buffers[ readLocation ];&lt;br /&gt;
            Console.Write( &amp;quot;\n{0} reads {1} &amp;quot;,Thread.CurrentThread.Name, buffers[ readLocation ] );&lt;br /&gt;
            --occupiedBufferCount;&lt;br /&gt;
            readLocation = ( readLocation + 1 ) % buffers.Length;&lt;br /&gt;
            Console.Write( CreateStateOutput() );&lt;br /&gt;
            Monitor.Pulse( this );&lt;br /&gt;
            return readValue;&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
      set&lt;br /&gt;
      {&lt;br /&gt;
         lock ( this )&lt;br /&gt;
         {&lt;br /&gt;
            if ( occupiedBufferCount == buffers.Length )&lt;br /&gt;
            {&lt;br /&gt;
               Console.Write( &amp;quot;\nAll buffers full. {0} waits.&amp;quot;,Thread.CurrentThread.Name );&lt;br /&gt;
               Monitor.Wait( this );&lt;br /&gt;
            }&lt;br /&gt;
            buffers[ writeLocation ] = value;&lt;br /&gt;
            Console.Write( &amp;quot;\n{0} writes {1} &amp;quot;,Thread.CurrentThread.Name, buffers[ writeLocation ] );&lt;br /&gt;
            ++occupiedBufferCount;&lt;br /&gt;
            writeLocation = ( writeLocation + 1 ) % buffers.Length;&lt;br /&gt;
            Console.Write( CreateStateOutput() );&lt;br /&gt;
            Monitor.Pulse( this );&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
   public string CreateStateOutput()&lt;br /&gt;
   {&lt;br /&gt;
      string output = &amp;quot;(buffers occupied: &amp;quot; + occupiedBufferCount + &amp;quot;)\nbuffers: &amp;quot;;&lt;br /&gt;
      for ( int i = 0; i &amp;lt; buffers.Length; i++ )&lt;br /&gt;
         output += &amp;quot; &amp;quot; + string.Format( &amp;quot;{0,2}&amp;quot;, buffers[ i ] ) + &amp;quot;  &amp;quot;;&lt;br /&gt;
      output += &amp;quot;\n&amp;quot;;&lt;br /&gt;
      output += &amp;quot;         &amp;quot;;&lt;br /&gt;
      for ( int i = 0; i &amp;lt; buffers.Length; i++ )&lt;br /&gt;
         output += &amp;quot;---- &amp;quot;;&lt;br /&gt;
      output += &amp;quot;\n&amp;quot;;&lt;br /&gt;
      output += &amp;quot;         &amp;quot;;&lt;br /&gt;
      for ( int i = 0; i &amp;lt; buffers.Length; i++ ) &lt;br /&gt;
      {&lt;br /&gt;
         if ( i == writeLocation &amp;amp;&amp;amp; &lt;br /&gt;
            writeLocation == readLocation ) &lt;br /&gt;
            output += &amp;quot; WR  &amp;quot;;&lt;br /&gt;
         else if ( i == writeLocation )&lt;br /&gt;
            output += &amp;quot; W   &amp;quot;;&lt;br /&gt;
         else if  ( i == readLocation ) &lt;br /&gt;
            output += &amp;quot;  R  &amp;quot;;&lt;br /&gt;
         else&lt;br /&gt;
            output += &amp;quot;     &amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
      output += &amp;quot;\n&amp;quot;;&lt;br /&gt;
      return output;&lt;br /&gt;
   }&lt;br /&gt;
   static void Main( string[] args )&lt;br /&gt;
   {&lt;br /&gt;
      CircularBuffer shared = new CircularBuffer();&lt;br /&gt;
      Random random = new Random();&lt;br /&gt;
      Console.Write( shared.CreateStateOutput() );&lt;br /&gt;
      Producer producer = new Producer( shared, random );&lt;br /&gt;
      Consumer consumer = new Consumer( shared, random );&lt;br /&gt;
      Thread producerThread = new Thread( new ThreadStart( producer.Produce ) );&lt;br /&gt;
      producerThread.Name = &amp;quot;Producer&amp;quot;;&lt;br /&gt;
      Thread consumerThread = new Thread( new ThreadStart( consumer.Consume ) );&lt;br /&gt;
      consumerThread.Name = &amp;quot;Consumer&amp;quot;;&lt;br /&gt;
      producerThread.Start();&lt;br /&gt;
      consumerThread.Start();&lt;br /&gt;
   }    &lt;br /&gt;
} &lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Uses Wait and Pulse to enable producer and consumer threads to cooperate in using a buffer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
Revised from code in &amp;quot;Computing with C# and the .NET Framework&amp;quot;&lt;br /&gt;
# Paperback: 753 pages&lt;br /&gt;
# Publisher: Jones and Bartlett Publishers, Inc.; Bk&amp;amp;CD-Rom edition (February 2003)&lt;br /&gt;
# Language: English&lt;br /&gt;
# ISBN: 0763723398&lt;br /&gt;
# Product Dimensions: 8.9 x 7.7 x 1.1 inches&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.rupilerServices;&lt;br /&gt;
using System.Threading;  &lt;br /&gt;
  class Buffer {&lt;br /&gt;
    public const int size = 3;&lt;br /&gt;
    int[] buffer = new int [size];             &lt;br /&gt;
    int putpos=0;&lt;br /&gt;
    int getpos=0;&lt;br /&gt;
    int number=0;&lt;br /&gt;
    [MethodImpl(MethodImplOptions.Synchronized)]&lt;br /&gt;
    public void Put(int value) {&lt;br /&gt;
      if (number == size) {&lt;br /&gt;
        Console.WriteLine(&amp;quot;Cannot put -- Buffer full&amp;quot;);&lt;br /&gt;
        Monitor.Wait(this);&lt;br /&gt;
      }&lt;br /&gt;
      number++;&lt;br /&gt;
      buffer[putpos] = value;&lt;br /&gt;
      Console.WriteLine(&amp;quot;Put &amp;quot;+value);&lt;br /&gt;
      putpos = (putpos + 1) % size;&lt;br /&gt;
      if (number == 1) Monitor.Pulse(this);         &lt;br /&gt;
    }&lt;br /&gt;
    [MethodImpl(MethodImplOptions.Synchronized)]&lt;br /&gt;
    public int Get() {&lt;br /&gt;
      if (number == 0) {&lt;br /&gt;
         Console.WriteLine(&amp;quot;Buffer empty&amp;quot;);&lt;br /&gt;
         Monitor.Wait(this);&lt;br /&gt;
      }&lt;br /&gt;
      number--;&lt;br /&gt;
      int n = buffer[getpos];&lt;br /&gt;
      Console.WriteLine(&amp;quot;Get &amp;quot;+n);&lt;br /&gt;
      getpos = (getpos + 1) % size;&lt;br /&gt;
      if (number == size - 1) Monitor.Pulse(this);    &lt;br /&gt;
      return n;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  class Producer {&lt;br /&gt;
     Buffer buf;&lt;br /&gt;
     public Producer(Buffer b) { &lt;br /&gt;
        buf = b;&lt;br /&gt;
        Thread thread = new Thread(new ThreadStart(Run));&lt;br /&gt;
        thread.Start();  &lt;br /&gt;
     }      &lt;br /&gt;
     public void Run() {&lt;br /&gt;
       for(; true; ) {&lt;br /&gt;
             buf.Put(0);&lt;br /&gt;
       }&lt;br /&gt;
     }&lt;br /&gt;
  }&lt;br /&gt;
  class Consumer {&lt;br /&gt;
     Buffer buf;&lt;br /&gt;
     public Consumer(Buffer b) { &lt;br /&gt;
       buf = b;&lt;br /&gt;
       Thread thread = new Thread(new ThreadStart(Run));&lt;br /&gt;
       thread.Start();  &lt;br /&gt;
     }&lt;br /&gt;
     public void Run() {&lt;br /&gt;
       for (; true;) {&lt;br /&gt;
           buf.Get();&lt;br /&gt;
       }&lt;br /&gt;
     }&lt;br /&gt;
  }&lt;br /&gt;
public class PutGet {&lt;br /&gt;
  public static void Main(String[] args) {&lt;br /&gt;
    Buffer b = new Buffer();&lt;br /&gt;
    Producer p = new Producer(b);&lt;br /&gt;
    Consumer c = new Consumer(b);&lt;br /&gt;
  }&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>