Richard's Coding Blog

Richard's Coding Blog

C# Threading

Testing multithreading

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)
        {
            //prompt for input - 4 methods use the same number passed
            Console.WriteLine("Please enter a number: ");

            //convert input into a double
            double input = Convert.ToDouble(Console.ReadLine());

            //create four threads
            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);

            //starts each thread
            foreach (Thread myThread in testThread)
            {
                //pass input into the four methods
                myThread.Start(input);
            }
        }

        //first thread method
        static void ThreadOne(object n)
        {
            //executing in thread

            //convert passed parameter to a double
            double threadOneNum = Convert.ToDouble(n);

            //initial variables
            int count = 0;
            double sum = 0;

            //if the passed parameter is between 0-10 ...
            if (threadOneNum >= 0 && threadOneNum <= 10)
            {
                //loops 10 times
                while (count++ < 10)
                {
                    //adds the passed number to the total sum with each loop
                    sum += threadOneNum;

                    //output at the end of each loop
                    Console.WriteLine("Thread 1 executed.");
                }
            }
            //if the passed parameter is greater than 10 and less than/equal to 20 ...
            else if (threadOneNum > 10 && threadOneNum <= 20)
            {
                //loops 20 times
                while (count++ < 20)
                {
                    //adds the passed number to the total sum with each loop
                    sum += threadOneNum;

                    //output at the end of each loop
                    Console.WriteLine("Thread 1 executed.");
                }
            }
            //if the passed parameter is neither of the 2 situations ...
            else
            {
                //loops 3 times
                while (count++ < 3)
                {
                    //adds the passed number to the total sum with each loop
                    sum += threadOneNum;

                    //output at the end of each loop
                    Console.WriteLine("Thread 1 executed.");
                }
            }

            //output
            Console.WriteLine("Thread 1 is finished. Final number: " + sum);
        }

        //second thread method
        static void ThreadTwo(object n)
        {
            //executing in thread

            //convert passed parameter to a double
            double threadTwoNum = Convert.ToDouble(n);

            //initial variables
            int count = 0;
            double sum = 0;

            //loops 5 times
            while (count++ < 5)
            {
                //with each loop, add the passed parameter times three
                sum += (threadTwoNum * 3);

                //output at the end of each loop
                Console.WriteLine("Thread 2 executed.");
            }

            //output
            Console.WriteLine("Thread 2 is finished. Final number: " + sum);
        }

        //third thread method
        static void ThreadThree(object n)
        {
            //executing in thread

            //convert passed parameter to a double
            double threadThreeNum = Convert.ToDouble(n);

            //initial variables
            int count = 0;
            double sum = 0;

            //loop 7 times
            while (count++ < 7)
            {
                //sum total becomes the passed parameter ...
                //plus the parameter multiplied by the count value
                sum = threadThreeNum + (threadThreeNum * count);

                //output at the end of each loop
                Console.WriteLine("Thread 3 executed.");
            }

            //output
            Console.WriteLine("Thread 3 is finished. Final number: " + sum);
        }

        //fourth thread method
        static void ThreadFour(object n)
        {
            //executing in thread

            //convert passed parameter to a double
            double threadFourNum = Convert.ToDouble(n);

            int count = 0;
            double sum = 0;

            //loop 3 times
            while (count++ < 3)
            {
                //adds the passed number to the sum
                sum = sum + threadFourNum;

                //multiplies the sum by 5
                sum = sum * 5;

                //divide the sum by 2
                sum = sum / 2;

                //subtract the value of the passed parameter from the current sum
                sum = sum - threadFourNum;

                //output at the end of each loop
                Console.WriteLine("Thread 4 executed.");
            }

            //output
            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": image.png

Same input, but I got a different result: image.png

Test using the number "15":

image.png

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.

image.png

Interesting.

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

 
Share this