Going through this article
class ThreadTest
{
static void Main()
{
Thread t = new Thread (WriteY); // Kick off a new thread
t.Start(); // running WriteY()
// Simultaneously, do something on the main thread.
for (int i = 0; i < 1000; i++) Console.Write ("x");
}
static void WriteY()
{
for (int i = 0; i < 1000; i++) Console.Write ("y");
}
}
Produces:
xxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyy
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
yyyyyyyyyyyyyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
...
Why isn’t it producing results like this?:
xyxyxyxyxyxxyyxyxxxyyxyxyx....
“A thread is an independent execution path, able to run simultaneously with other threads.”
The above doesn’t exactly look simultaneous to me.
The two threads cannot run concurrently because they both do nothing but repeatedly access the same resource. When one thread is running, the other thread is almost certainly waiting for the console and cannot run. Test with two threads that do something other than exclusively access precisely the same unsharable resource.
You and me can do errands concurrently. But if the only errands you and I need to do require the same car, then we’ll wind up taking turns. And you won’t just do one errand and then bring the car back so I can do one errand. That would be ridiculous and we’d spend all our time bringing the car back. You’d do a few errands, then I’d do a few.