Now, i am learning multi-threading and usage of it in C#. So, i face the problem as below: (Sorry for my so simple question)
Suppose that, we have two classes named Producer and Consumer. Producer task is producing 4 numbers while program running and Consumer task is consuming and using those numbers and return the sum of them at the end of program.
Consumer Class definition:
class Consumer
{
    private HoldInteger sharedLocation;
    private Random randomSleepTime;
    public Consumer(HoldInteger shared, Random random)
    {
        sharedLocation = shared;
        randomSleepTime = random;
    }
    public void Consume()
    {
        int sum = 0;
        for (int i = 1; i <= 4; i++)
        {
            Thread.Sleep(randomSleepTime.Next(1, 3000));
            sum += sharedLocation.Buffer;
        }
    }
}
and the definition of Producer Class is as below :
class Producer
{
    private HoldInteger sharedLocation;
    private Random randomSleepTime;
    public Producer(HoldInteger shared, Random random)
    {
        sharedLocation = shared;
        randomSleepTime = random;
    }
    public void Produce()
    {
        for (int i = 1; i <= 4; i++)
        {
            Thread.Sleep(randomSleepTime.Next(1, 3000));
            sharedLocation.Buffer = i;
        }
    }
}
And also, we have HoldInteger class contains Buffer variable that producer write this variable and consumer read from that. I combine these classes and program the below code in my main method:
static void Main(string[] args)
{
   HoldInteger holdInteger = new HoldInteger();
   Random random = new Random();
   Producer producer = new Producer(holdInteger, random);
   Consumer consumer = new Consumer(holdInteger, random);
   Thread producerThread = new Thread(new ThreadStart(producer.Produce));
   producerThread.Name = "producer";
   Thread consumerThread = new Thread(new ThreadStart(consumer.Consume));
   consumerThread.Name = "consumer";
   producerThread.Start();
   consumerThread.Start();
}
So, my question is that How can i manage this relationship With Low Memory and Time Wasting ?
Please note that, these threads management code will be placed in HoldInteger class body.
Thanks for your attention.
 
     
     
    