When multiple threads using the same resource produce different outcomes due to the timings of each thread, this is known as a race condition. We will demonstrate this in action now.
In our demonstration, we will have two threads. Each thread will call a method to print the alphabet. One method will print the alphabet using uppercase letters. The second method will print the alphabet using lowercase letters. From the demonstration, we'll see how the output is wrong, and every time the program is run, the output will be wrong.
First, add the ThreadingRaceCondition() method:
static void ThreadingRaceCondition()
{
Thread T1 = new Thread(Method1);
T1.Start();
Thread T2 = new Thread(Method2);
T2.Start();
}
ThreadingRaceCondition() produces two threads and starts them. It also references two methods. Method1() prints out the alphabet in uppercase and Method2() prints out the alphabet...