Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6347035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:07:53+00:00 2026-05-24T21:07:53+00:00

Now, i am learning multi-threading and usage of it in C#. So, i face

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:07:55+00:00Added an answer on May 24, 2026 at 9:07 pm

    I would replace the HoldInteger class with a BlockingQueue, you can find an implementation here and for more details on the reason behind the implementation, check this question. I think .NET 4.0 might have a blocking queue too. This approach will subsequently make things a lot easier to manage:

    class Producer
    {
        //...
    
        public void Produce()
        {
            for (int i = 1; i <= 4; i++)
            {
                Thread.Sleep(randomSleepTime.Next(1, 3000));
                blockingIntQueue.Enqueue(i);
            }
        }
    }
    

    Your consumer will look like this now:

    class Consumer
    {
        //...
    
        public void Consume()
        {
            int value = 0;
            for (int i = 1; i <= 4; i++)
            {
                if( blockingIntQueue.TryDequeue(out value) )
                {
                    sum += value;
                }
            }
        }
    }
    

    However, if you want to keep the HoldInteger (if this is some sort of requirement), then you can place the blocking queue inside the HoldIntegerUnsynchronized class instead of having a buffer (should be trivial to do) and you will achieve the same result.

    Note: with this approach you no longer have to worry about missing a value or reading a stale value because the threads don’t wake up at exactly the right time. Here is the potential problem with using a “buffer”:

    Even if your integer holder does handle the underlying “buffer” safely, you are still not guaranteed that you will get all the integers that you want. Take this into consideration:

    Case 1

    Producer wakes up and writes integer.
    Consumer wakes up and reads integer.
    
    Consumer wakes up and reads integer.
    Producer wakes up and writes integer.
    

    Case 2

    Consumer wakes reads integer.
    Producer wakes up and writes integer.
    
    Producer wakes up and writes integer.
    Consumer wakes up and reads integer.
    

    Since the timer is not precise enough, this sort of thing is entirely possible and in the first case it will cause the consumer to read a stale value, while in the second case it will cause the consumer to miss a value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm now learning a bit of MATLAB and I have two versions of it.
We have a new developer that is just now learning LinqToSql. While going over
I'm learning Python now because of the Django framework. I have been a Perl
I have committed to learning C now, I'm good with Python/PHP/Bash but I've decided
I have been progressing through Learn Prolog Now! as self-study and am now learning
I am just learning how to write mutithreaded programs right now and I have
I'm learning Java and was able to do a bit of multi-threading with my
I'm just now learning about file tasks in Rake. I have 100s of javascript
I have been in at the deep end for a week now learning from
I have mostly programmed in Python, but I am now learning the statistical programming

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.