2016年1月26日 星期二

C17_9 生產者與消費者問題 Produce.cs

using System;
public class Produce
{
    Warehouse warehouse;
    //生產者生產次數,初始畫為1
    int quantity = 1;
    public Produce(Warehouse box, int request)
    {
        warehouse = box;
        quantity = request;
    }
    public void Threadrun()
    {
        for (int i = 1; i <= quantity; i++)
        {
            //生產者向操作物件寫入資訊
            warehouse.SetProduce(i);
        }
    }
}
public class Consumer
{
    Warehouse warehouse;
    int quantity = 1;
    public Consumer(Warehouse box, int request)
    {
        warehouse = box;
        quantity = request;
    }
    public void ThreadRun()
    {
        int valReturned;
        for (int i = 1; i <= quantity; i++)
        {
            //消費者從操作物件中讀取資訊
            valReturned = warehouse.GetProduce();
        }
    }
}

沒有留言: