<?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%2FCSharp_Tutorial%2FThread%2FProducer_Consumer</id>
		<title>Csharp/CSharp Tutorial/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%2FCSharp_Tutorial%2FThread%2FProducer_Consumer"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Producer_Consumer&amp;action=history"/>
		<updated>2026-04-29T17:45:16Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Producer_Consumer&amp;diff=6676&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Producer_Consumer&amp;diff=6676&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:53Z</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/CSharp_Tutorial/Thread/Producer_Consumer&amp;diff=6677&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Producer_Consumer&amp;diff=6677&amp;oldid=prev"/>
				<updated>2010-05-26T12:20:13Z</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;==Consumer Producer with Monitor==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
public class MyData {&lt;br /&gt;
    private double _pi = 0.0;&lt;br /&gt;
    private int _iterations = 0;&lt;br /&gt;
    private bool _valueReady = false;&lt;br /&gt;
    public void WriteData(int iterations, double val) {&lt;br /&gt;
        lock (this) {&lt;br /&gt;
            if (_valueReady) {&lt;br /&gt;
                Monitor.Wait(this);&lt;br /&gt;
            }&lt;br /&gt;
            _pi = val;&lt;br /&gt;
            _iterations = iterations;&lt;br /&gt;
            _valueReady = true;&lt;br /&gt;
            Monitor.Pulse(this);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public void ReadData(out int iterations, out double val) {&lt;br /&gt;
        lock (this) {&lt;br /&gt;
            if (!_valueReady) {&lt;br /&gt;
                Monitor.Wait(this);&lt;br /&gt;
            }&lt;br /&gt;
            val = _pi;&lt;br /&gt;
            iterations = _iterations;&lt;br /&gt;
            _valueReady = false;&lt;br /&gt;
            Monitor.Pulse(this);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class MyProducer {&lt;br /&gt;
    private MyData _pi;&lt;br /&gt;
    private int TotalIters;&lt;br /&gt;
    public MyProducer(MyData pi, int iterations) {&lt;br /&gt;
        _pi = pi;&lt;br /&gt;
        TotalIters = iterations;&lt;br /&gt;
    }&lt;br /&gt;
    public Thread CreateProducerThread() {&lt;br /&gt;
        return new Thread(new ThreadStart(this.calculate));&lt;br /&gt;
    }&lt;br /&gt;
    private void calculate() {&lt;br /&gt;
        int iters = 1;&lt;br /&gt;
        do {&lt;br /&gt;
            iters += 4;&lt;br /&gt;
            _pi.WriteData(iters, iters * 4);&lt;br /&gt;
        } while (iters &amp;lt; TotalIters);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class MyConsumer {&lt;br /&gt;
    private MyData _pi;&lt;br /&gt;
    private int TotalIters;&lt;br /&gt;
    public MyConsumer(MyData pi, int iterations) {&lt;br /&gt;
        _pi = pi;&lt;br /&gt;
        TotalIters = iterations;&lt;br /&gt;
    }&lt;br /&gt;
    public Thread CreateConsumerThread() {&lt;br /&gt;
        return new Thread(new ThreadStart(this.printValues));&lt;br /&gt;
    }&lt;br /&gt;
    private void printValues() {&lt;br /&gt;
        int iters = new int();&lt;br /&gt;
        double pi = new double();&lt;br /&gt;
        do {&lt;br /&gt;
            _pi.ReadData(out  iters, out pi);&lt;br /&gt;
            System.Console.WriteLine(&amp;quot;Iters: {0}\tPi:  {1}&amp;quot;,iters.ToString(), pi.ToString());&lt;br /&gt;
        } while (iters &amp;lt; TotalIters);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class MainClass {&lt;br /&gt;
    static void Main(string[] args) {&lt;br /&gt;
        MyData pi = new MyData();&lt;br /&gt;
        MyProducer prod = new MyProducer(pi, 100000);&lt;br /&gt;
        Thread producerThread = prod.CreateProducerThread();&lt;br /&gt;
        MyConsumer cons = new MyConsumer(pi, 100000);&lt;br /&gt;
        Thread consumerThread = cons.CreateConsumerThread();&lt;br /&gt;
        producerThread.Start();&lt;br /&gt;
        consumerThread.Start();&lt;br /&gt;
        producerThread.Join();&lt;br /&gt;
        consumerThread.Join();&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Philosopher Example==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
class PhilosopherExample {&lt;br /&gt;
    public static bool chopStick1Available = true;&lt;br /&gt;
    public static bool chopStick2Available = true;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        Thread philosopher1 = new Thread(new ThreadStart(GetChopSticks1));&lt;br /&gt;
        Thread philosopher2 = new Thread(new ThreadStart(GetChopSticks2));&lt;br /&gt;
        philosopher1.Start();&lt;br /&gt;
        philosopher2.Start();&lt;br /&gt;
    }&lt;br /&gt;
    public static void GetChopSticks1() {&lt;br /&gt;
        while (!chopStick1Available) {&lt;br /&gt;
            Console.WriteLine(&amp;quot;#1 waiting for 1st&amp;quot;);&lt;br /&gt;
            Thread.Sleep(100);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;#1 got 1st&amp;quot;);&lt;br /&gt;
        chopStick1Available = false;&lt;br /&gt;
        while (!chopStick2Available) {&lt;br /&gt;
            Console.WriteLine(&amp;quot;#1 waiting for 2nd&amp;quot;);&lt;br /&gt;
            Thread.Sleep(100); &lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;#1 got 2nd&amp;quot;);&lt;br /&gt;
        chopStick2Available = false;&lt;br /&gt;
        Console.WriteLine(&amp;quot;#1 uses and releases chopsticks.&amp;quot;);&lt;br /&gt;
        chopStick1Available = true;&lt;br /&gt;
        chopStick2Available = true;&lt;br /&gt;
    }&lt;br /&gt;
    public static void GetChopSticks2() {&lt;br /&gt;
        while (!chopStick2Available) {&lt;br /&gt;
            Console.WriteLine(&amp;quot;#2 waiting for 1st&amp;quot;);&lt;br /&gt;
            Thread.Sleep(100);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;#2 got 1st chopstick.&amp;quot;);&lt;br /&gt;
        chopStick2Available = false;&lt;br /&gt;
        while (!chopStick1Available) {&lt;br /&gt;
            Console.WriteLine(&amp;quot;#2 waiting for 2nd&amp;quot;);&lt;br /&gt;
            Thread.Sleep(100);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;#2 got 2nd chopstick.&amp;quot;);&lt;br /&gt;
        chopStick1Available = false;&lt;br /&gt;
        Console.WriteLine(&amp;quot;#2 uses and releases chopsticks.&amp;quot;);&lt;br /&gt;
        chopStick1Available = true;&lt;br /&gt;
        chopStick2Available = true;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Producer and consumer==&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;
Quote from &lt;br /&gt;
C# and the .NET Framework&lt;br /&gt;
by Bob Powell &lt;br /&gt;
*/&lt;br /&gt;
using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
public class Factory {&lt;br /&gt;
  private int[]  Widgets = new int[100];&lt;br /&gt;
  private int    WidgetIndex = 0;&lt;br /&gt;
  private AutoResetEvent NewWidgetEvent = new AutoResetEvent( false );&lt;br /&gt;
  protected void Producer( ) {&lt;br /&gt;
    while( true ) {&lt;br /&gt;
      lock( this ) {&lt;br /&gt;
        if( WidgetIndex &amp;lt; 100 ) {&lt;br /&gt;
          Widgets[ WidgetIndex ] = 1;&lt;br /&gt;
          Console.WriteLine(&amp;quot;Widget {0} Produced&amp;quot;, WidgetIndex++ );&lt;br /&gt;
          NewWidgetEvent.Set( );&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
      Thread.Sleep( (new Random()).Next( 5 ) * 1000 );&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  protected void Consumer( ) {&lt;br /&gt;
    while( true ) {&lt;br /&gt;
      NewWidgetEvent.WaitOne( );&lt;br /&gt;
      int iWidgetIndex = 0;&lt;br /&gt;
      lock( this ) {&lt;br /&gt;
        iWidgetIndex = --this.WidgetIndex;&lt;br /&gt;
        Console.WriteLine(&amp;quot;Consuming widget {0}&amp;quot;, iWidgetIndex );  &lt;br /&gt;
        Widgets[ iWidgetIndex-- ] = 0;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void Run( ) {&lt;br /&gt;
    for( int i = 0; i &amp;lt; 3; i++ ) {&lt;br /&gt;
      Thread producer = new Thread( new ThreadStart( Producer ) );&lt;br /&gt;
      producer.Start( );&lt;br /&gt;
    }&lt;br /&gt;
    for( int i = 0; i &amp;lt; 3; i++ ) {&lt;br /&gt;
      Thread consumer = new Thread( new ThreadStart( Consumer ) );&lt;br /&gt;
      consumer.Start( );&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public static void Main( ) {&lt;br /&gt;
    Factory factory = new Factory( );&lt;br /&gt;
    factory.Run( );&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Widget 0 Produced&lt;br /&gt;
Widget 1 Produced&lt;br /&gt;
Widget 2 Produced&lt;br /&gt;
Consuming widget 2&lt;br /&gt;
Widget 2 Produced&lt;br /&gt;
Consuming widget 2&lt;br /&gt;
Widget 2 Produced&lt;br /&gt;
Widget 3 Produced&lt;br /&gt;
Consuming widget 3&lt;br /&gt;
Consuming widget 2&lt;br /&gt;
Widget 2 Produced&lt;br /&gt;
Consuming widget 2&lt;br /&gt;
Widget 2 Produced&lt;br /&gt;
Widget 3 Produced&lt;br /&gt;
Consuming widget 3&lt;br /&gt;
Consuming widget 2&lt;br /&gt;
Widget 2 Produced&lt;br /&gt;
Consuming widget 2&lt;br /&gt;
^CTerminate batch job (Y/N)? n&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Producer/consumer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Reflection;&lt;br /&gt;
using System.Runtime;&lt;br /&gt;
using System.Runtime.rupilerServices;&lt;br /&gt;
using System.Security;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    private static Queue&amp;lt;string&amp;gt; sharedQueue = new Queue&amp;lt;string&amp;gt;();    &lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        Thread t1 = new Thread(Producer);&lt;br /&gt;
        Thread t2 = new Thread(Consumer);&lt;br /&gt;
        t1.Start();&lt;br /&gt;
        t2.Start();&lt;br /&gt;
        // Join on them:&lt;br /&gt;
        t1.Join();&lt;br /&gt;
        t2.Join();&lt;br /&gt;
    }&lt;br /&gt;
    private static void Producer()&lt;br /&gt;
    {&lt;br /&gt;
        for (int i = 0; i &amp;lt; 2; i++)&lt;br /&gt;
        {&lt;br /&gt;
            string item = &amp;quot;Item#&amp;quot; + i;&lt;br /&gt;
            lock (sharedQueue)&lt;br /&gt;
            {&lt;br /&gt;
                sharedQueue.Enqueue(item);&lt;br /&gt;
                Monitor.Pulse(sharedQueue);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    private static void Consumer()&lt;br /&gt;
    {&lt;br /&gt;
        for (int i = 0; i &amp;lt; 2; i++)&lt;br /&gt;
        {&lt;br /&gt;
            string item = null;&lt;br /&gt;
            lock (sharedQueue)&lt;br /&gt;
            {&lt;br /&gt;
                while (sharedQueue.Count == 0)&lt;br /&gt;
                    Monitor.Wait(sharedQueue);&lt;br /&gt;
                item = sharedQueue.Dequeue();&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;Processing item: {0}&amp;quot;, item);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Processing item: Item#0&lt;br /&gt;
Processing item: Item#1&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>