Monitor

In this chapter you will learn:

  1. Consumer Producer with Monitor

Consumer Producer with Monitor

using System;//from  j  a v  a  2s  .c  o m
using System.Threading;

public class MyData {
    private double _pi = 0.0;
    private int _iterations = 0;
    private bool _valueReady = false;

    public void WriteData(int iterations, double val) {
        lock (this) {
            if (_valueReady) {
                Monitor.Wait(this);
            }
            _pi = val;
            _iterations = iterations;
            _valueReady = true;
            Monitor.Pulse(this);
        }
    }

    public void ReadData(out int iterations, out double val) {
        lock (this) {
            if (!_valueReady) {
                Monitor.Wait(this);
            }
            val = _pi;
            iterations = _iterations;
            _valueReady = false;
            Monitor.Pulse(this);
        }
    }
}

class MyProducer {
    private MyData _pi;
    private int TotalIters;

    public MyProducer(MyData pi, int iterations) {
        _pi = pi;
        TotalIters = iterations;
    }

    public Thread CreateProducerThread() {
        return new Thread(new ThreadStart(this.calculate));
    }
    private void calculate() {
        int iters = 1;
        do {
            iters += 4;
            _pi.WriteData(iters, iters * 4);
        } while (iters < TotalIters);
    }
}

class MyConsumer {
    private MyData _pi;
    private int TotalIters;

    public MyConsumer(MyData pi, int iterations) {
        _pi = pi;
        TotalIters = iterations;
    }

    public Thread CreateConsumerThread() {
        return new Thread(new ThreadStart(this.printValues));
    }

    private void printValues() {
        int iters = new int();
        double pi = new double();
        do {
            _pi.ReadData(out  iters, out pi);
            System.Console.WriteLine("Iters: {0}\tPi:  {1}",iters.ToString(), pi.ToString());
        } while (iters < TotalIters);
    }
}

class MainClass {
    static void Main(string[] args) {
        MyData pi = new MyData();
        MyProducer prod = new MyProducer(pi, 100000);
        Thread producerThread = prod.CreateProducerThread();
        MyConsumer cons = new MyConsumer(pi, 100000);
        Thread consumerThread = cons.CreateConsumerThread();
        producerThread.Start();
        consumerThread.Start();

        producerThread.Join();
        consumerThread.Join();
    }
}

Next chapter...

What you will learn in the next chapter:

  1. How to create Mutex
  2. Threading with Mutex
  3. Use a Mutex to control a shared resource against two current threads
Home » C# Tutorial » Thread
Thread creation
ThreadStart
Make a thread to sleep
Join thread
Thread attribute
Thread priorities
Thread State
Background thread
Thread interupting
Monitor
Mutex