CHAPTER 19 MULTITHREADED, PARALLEL, AND ASYNC PROGRAMMING
The Main() method is responsible for creating an array of ten (uniquely named) Thread objects, each
of which is making calls on the same instance of the Printer object as follows:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*****Synchronizing Threads *****\n");
Printer p = new Printer();
// Make 10 threads that are all pointing to the same
// method on the same object.
Thread[] threads = new Thread[10];
for (int i = 0; i < 10; i++)
{
threads[i] =
new Thread(new ThreadStart(p.PrintNumbers));
threads[i].Name = string.Format("Worker thread #{0}", i);
}
}
// Now start each one.
foreach (Thread t in threads)
t.Start();
Console.ReadLine();
}
Before looking at some test runs, let’s recap the problem. The primary thread within this
AppDomain begins life by spawning ten secondary worker threads. Each worker thread is told to make
calls on the PrintNumbers() method on the same Printer instance. Given that you have taken no
precautions to lock down this object’s shared resources (the console), there is a good chance that the
current thread will be kicked out of the way before the PrintNumbers() method is able to print out the
complete results. Because you don’t know exactly when (or if) this might happen, you are bound to get
unpredictable results. For example, you might find the output shown here:
*****Synchronizing Threads *****
-> Worker thread #1 is executing PrintNumbers()
Your numbers: -> Worker thread #0 is executing PrintNumbers()
-> Worker thread #2 is executing PrintNumbers()
Your numbers: -> Worker thread #3 is executing PrintNumbers()
Your numbers: -> Worker thread #4 is executing PrintNumbers()
Your numbers: -> Worker thread #6 is executing PrintNumbers()
Your numbers: -> Worker thread #7 is executing PrintNumbers()
Your numbers: -> Worker thread #8 is executing PrintNumbers()
Your numbers: -> Worker thread #9 is executing PrintNumbers()
Your numbers: Your numbers: -> Worker thread #5 is executing PrintNumbers()
Your numbers: 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 2, 1, 0, 0, 4, 3,
4, 1, 2, 4, 5, 5, 5, 6, 6, 6, 2, 7, 7, 7, 3, 4, 0, 8, 4, 5, 1, 5, 8, 8, 9,
2, 6, 1, 0, 9, 1,
721