The idea of threading is to execute code simultaneously.
What I'm doing down below is testing the task prioritization of the four threads that I made. Each of the four threads will take in a number as input and will use that number to perform different operations.
using System;
using System.Threading;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter a number: ");
double input = Convert.ToDouble(Console.ReadLine());
Thread thread1 = new Thread(ThreadOne);
Thread thread2 = new Thread(ThreadTwo);
Thread thread3 = new Thread(ThreadThree);
Thread thread4 = new Thread(ThreadFour);
Thread[] testThread = new Thread[4];
testThread[0] = new Thread(ThreadOne);
testThread[1] = new Thread(ThreadTwo);
testThread[2] = new Thread(ThreadThree);
testThread[3] = new Thread(ThreadFour);
foreach (Thread myThread in testThread)
{
myThread.Start(input);
}
}
static void ThreadOne(object n)
{
double threadOneNum = Convert.ToDouble(n);
int count = 0;
double sum = 0;
if (threadOneNum >= 0 && threadOneNum <= 10)
{
while (count++ < 10)
{
sum += threadOneNum;
Console.WriteLine("Thread 1 executed.");
}
}
else if (threadOneNum > 10 && threadOneNum <= 20)
{
while (count++ < 20)
{
sum += threadOneNum;
Console.WriteLine("Thread 1 executed.");
}
}
else
{
while (count++ < 3)
{
sum += threadOneNum;
Console.WriteLine("Thread 1 executed.");
}
}
Console.WriteLine("Thread 1 is finished. Final number: " + sum);
}
static void ThreadTwo(object n)
{
double threadTwoNum = Convert.ToDouble(n);
int count = 0;
double sum = 0;
while (count++ < 5)
{
sum += (threadTwoNum * 3);
Console.WriteLine("Thread 2 executed.");
}
Console.WriteLine("Thread 2 is finished. Final number: " + sum);
}
static void ThreadThree(object n)
{
double threadThreeNum = Convert.ToDouble(n);
int count = 0;
double sum = 0;
while (count++ < 7)
{
sum = threadThreeNum + (threadThreeNum * count);
Console.WriteLine("Thread 3 executed.");
}
Console.WriteLine("Thread 3 is finished. Final number: " + sum);
}
static void ThreadFour(object n)
{
double threadFourNum = Convert.ToDouble(n);
int count = 0;
double sum = 0;
while (count++ < 3)
{
sum = sum + threadFourNum;
sum = sum * 5;
sum = sum / 2;
sum = sum - threadFourNum;
Console.WriteLine("Thread 4 executed.");
}
Console.WriteLine("Thread 4 is finished. Final number: " + sum);
}
}
}
Thread 1 will check if the number is:
- between 0-10
- greater than 10, but less than or equal to 20
If the first condition is met, the thread will loop 10 times. If the second condition is met, the thread will loop 20 times. If neither conditions are met, the thread will loop 3 times.
For Thread 2, there is no condition like in Thread 1.
However, Thread 2 takes the number input and multiplies it by 3 before adding it to the total.
The thread will loop this 5 times, in total.
Thread 3 also has no conditions like in Thread 1.
It takes the number, multiplies it by the number in the loop (starts at 0)
and then add it to the input number again before adding it to the total. It will perform this 7 times.
For example, if the input number is 4, the number added to the sum will be 4.
In the next loop, the number added will be 8 and then 12 afterwards.
Like Thread 2 and 3, there is no condition for Thread 4.
It will complete 3 loops. In each loop, it will perform the following steps:
- Add the input number to the sum
- Multiply the current sum by 5
- Divide the current sum by 2
- Subtract the input number from the current sum
While all threads create a sum total at the end, they each take different steps to completing their tasks. The point of this test is to see how the application handles the work simultaneously.
Initial test run using the number "5":

Same input, but I got a different result:

Test using the number "15":

We are completing the second condition in Thread 1, which means that the thread would loop an extra 10 times.
Understandably, Thread 1 completed the task after all the other threads. Weirdly enough, Thread 2 and Thread 3 finished their tasks much earlier than expected. The first two tests showed me that all four threads finish at just about the same time.
Let us try that again.

Interesting.
Thread 1 is the first one to finish their task this time. I wonder how that works.