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 398557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:45:37+00:00 2026-05-12T16:45:37+00:00

I am a beginner.Based on the Albahari’s Producer/Consumer Solution , I have developed a

  • 0

I am a beginner.Based on the Albahari’s Producer/Consumer Solution , I have developed a code.Coding is working fine.But I have some doubts on my implementation.

class FloodDisaterManagement :IDisposable
    {

        Queue<string>MedicinePack  = new Queue<string>();
        Thread[] volunteers;
        readonly object locker = new object();

    public FloodDisaterManagement(int volunteerCount)
    {
        volunteers = new Thread[volunteerCount];

        for (int i = 0; i < volunteerCount; i++)
        {
            volunteers[i] = new Thread(SupplyMedicine);
            volunteers[i].Start();
        }
    }

    public void DumpMedicine(string pack)
    {
        lock (locker)
        {
            MedicinePack.Enqueue(pack);
            Monitor.PulseAll(locker);
        }
    }

    public void Dispose()
    {

        foreach (Thread volunteer in volunteers) DumpMedicine(null);
        foreach (Thread volunteer in volunteers) volunteer.Join();
    }

    public void SupplyMedicine()
    {
        while (true)
        {
            string pack;
            lock (locker)
            {
                while (MedicinePack .Count == 0) Monitor.Wait(locker);
                pack= MedicinePack.Dequeue();
            }
            if (pack == null) return; 
            Console.WriteLine(pack+"is supplied");
            Thread.Sleep(1000); 
        }

    }


    static void Main()
    {
       string[] medicinePacks = new string[]  
        { "Pack1", "Pack2", "Pack3", "Pack4", "Pack5", "Pack6", 
          "Pack7", "Pack8", "Pack9", "Pack10"
        };

        using (FloodDisaterManagement q = new FloodDisaterManagement(2))
        {
            foreach (string pack in medicinePacks)
                q.DumpMedicine(pack);
            Console.WriteLine("Enqueued 10 Medicine Packs");

        }

        Console.WriteLine("..Medicines Distribution Completed");
        Console.ReadKey(true);
    }


}

Questions:


  1. As We have Using(…) in Main() ,Why still there is a need for Dispose() in
    FloodDisasterManagement class ?
  2. What is the exact purpose of having two foreach inside Dispose() ?
  3. Where does the control return when we use “return statement” inside the
    SupplyMedicine()? (i.e)
public void SupplyMedicine()
{
    while (true)
    {
        ....  
        if (pack == null) return; 
        ... 
       //Whether the control return to Main() or SomeWhere?
    }

}
  • 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-12T16:45:37+00:00Added an answer on May 12, 2026 at 4:45 pm

    1: because the using calls the Dispose(); that is the purpose of the using.

    2: to close each of the encapsulated resources, in this case threads (asking them to exit, then waiting for them to exit).

    3: it returns to the caller; in this case the caller is a Thread, so this cleanly exits the thread

    Or in English:

    • the using in this case acts to wait for all the threads to exit; without the using we would close too early
    • the foreach signals each thread to exit when it has finished processing its load, and waits for them all
    • the null is detected as a “now exit” signal, causing each thread to exit
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Beginner in Android development. My code crashes. I have made a simple Java method
I am working on a jQuery-based todo list interface, and have hit a bit
This is a very beginner question. I have tried to search for advice but
Total beginner with PHP: I have an form based on table data that the
I'm a beginner with Atlas Transformation Language and have already made some steps with
Beginner iOS dev here. I have a problem in my array. Im making an
Beginner here trying to get a pipeline working in bash. If somebody can see
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Beginner question: I have a dictionary where the values are lists of (a variable
Python beginner here, I have a list of lists and want to refer to

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.