C# MULTI-THREADING DEMO

###########################################################################
#
# NAME: MThreadingDemo
#
# AUTHOR:  SivaMulpuru
#
# COMMENT: This Program illustrates the advantage of using multithreading
#
# VERSION HISTORY:
# 1.0 5/4/2011 - Initial release
#
###########################################################################

using System;
using System.Threading;
using System.Diagnostics; //for Stopwatch
 
 
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Processor Count is {0}\n"Environment.ProcessorCount);
        Stopwatch stopwatch = new Stopwatch();
        Console.WriteLine("#################SingleThread####################");
        stopwatch.Start();
        Console.WriteLine("Fibonacci of {0} is {1}", 40, Fibonacci(40).ToString());
        Console.WriteLine("Fibonacci of {0} is {1}", 41, Fibonacci(41).ToString());
        stopwatch.Stop();
   Console.WriteLine("Time taken for Fibonacci of {0} and {1} asynchronously is {2} secs",
                            40, 41, stopwatch.ElapsedMilliseconds / 1000);
        Console.WriteLine("#################SingleThreadEnd#################\n");
 
 
        Console.WriteLine("#################TwoThreads####################");
 
        int[] x = new int[2];
        Thread[] threads = new Thread[2];
        x[0] = 40;
        x[1] = 41;
        stopwatch.Restart();
        for (int i = 0; i < x.Length; i++)
        {
            threads[i] = new Thread(DoMath);
            threads[i].Start(x[i]);
        }
        for (int i = 0; i < x.Length; i++)
        {
            threads[i].Join();
        }
        stopwatch.Stop();
   Console.WriteLine("Time taken for Fibonacci of {0} and {1} synchronously is {2} secs",
                            40, 41, stopwatch.ElapsedMilliseconds / 1000);
        Console.WriteLine("#################TwoThreadsEnd#################");
 
        Console.ReadLine();
    }
 
    static void DoMath(object n)
    {
        int _n = (int)n;
        Console.WriteLine("Fibonacci of {0} is {1}; Thead ID {2}", 
            _n, Fibonacci(_n).ToString(), Thread.CurrentThread.ManagedThreadId);
 
    }
 
    static long Fibonacci(int x)
    {
        if (x <= 1)
            return 1;
        return Fibonacci(x - 1) + Fibonacci(x - 2);
    }
}

Output

Processor Count is 4

#################SingleThread####################
Fibonacci of 40 is 165580141
Fibonacci of 41 is 267914296
Time taken for Fibonacci of 40 and 41 asynchronously is 11 secs
#################SingleThreadEnd#################

#################TwoThreads####################
Fibonacci of 40 is 165580141; Thead ID 3
Fibonacci of 41 is 267914296; Thead ID 4
Time taken for Fibonacci of 40 and 41 synchronously is 7 secs
#################TwoThreadsEnd#################

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s