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 (FloodDisaterMana开发者_开发技巧gement 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:
- As We have Using(...) in Main() ,Why still there is a need for Dispose() in FloodDisasterManagement class ?
- What is the exact purpose of having two foreach inside Dispose() ?
- 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: 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 theusing
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
About 3:
Since the SupplyMedicine method runs on a separate thread, and this is the first and only method run by the thread- the return statement simply means that this thread finished its work and should be terminated (or returned to the thread pool in case you are using a thread pool thread).
精彩评论