using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ch14_4
{
    class Consumer
    {
        private Buffer sharedLocation;
        private Random randomSleepTime;

        // constructor
        public Consumer(Buffer shared, Random random)
        {
            sharedLocation = shared;
            randomSleepTime = random;
        } // end constructor

        // read sharedLocation's value five times
        public void Consume()
        {
            int sum = 0;

            // sleep for random interval up to 3000 milliseconds then
            // add sharedLocation's Buffer property value to sum
            for (int count = 1; count <= 5; count++)
            {
                Thread.Sleep(randomSleepTime.Next(1, 3)); 
                
                sum += sharedLocation.Buffer;
            } // end for
            CommonClass.output  += "\n";
            CommonClass.output += Thread.CurrentThread.Name + "read values totaling: " + sum;
            CommonClass.output += "\n" + Thread.CurrentThread.Name + " Terminating\n";
        } // end method Consume
    }
}
