Tuesday, 12 January 2016

                 Multi-Threading and Asynchronous                                                                    (Part 2) 

 In this blog, we will discuss about Join method , foreground thread and background thread and ParameterizedThreadStart.                              
1. Join Method
Join is synchronous method that blocks the other thread until the thread which is on the CPU terminates.
Example:
Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace SimpleThread
{
    class Program
    {
        //Thread Method
        public static  void ThreadMethod()
        {
            //Loop
            for (int i = 0; i <= 5; i++)
            {
                Console.WriteLine("Thread 0");
                
            }
                
        }
        public static void ThreadMethod1()
        {
            //Loop
            for (int i = 0; i <= 5; i++)
            {
                Console.WriteLine("Thread 1");

            }


        }

        static void Main(string[] args)
        {
            
            //Thread Declartion and calling 
            Thread thread = new Thread(new ThreadStart(ThreadMethod));
            thread.Start();//Start Thread
            Thread thread1 = new Thread(new ThreadStart(ThreadMethod1));  //thread for Method2
            thread1.Start();//Start Thread
            Console.ReadKey(); //For holding Screen
        }
    }

}

OutPut1:
OutPut2:
Note:Every time you compile the code,you might get the different answer.So do not worry about that.
Now lets look what happen with the use of Join method.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace SimpleThread
{
    class Program
    {
        //Thread Method
        public static  void ThreadMethod()
        {
            //Loop
            for (int i = 0; i <= 5; i++)
            {
                Console.WriteLine("Thread 0");
                
            }
                
        }
        public static void ThreadMethod1()
        {
            //Loop
            for (int i = 0; i <= 5; i++)
            {
                Console.WriteLine("Thread 1");

            }


        }

        static void Main(string[] args)
        {
            
            //Thread Declartion and calling 
            Thread thread = new Thread(new ThreadStart(ThreadMethod));
            thread.Start();//Start Thread
thread.Join();    //Join Method
            Thread thread1 = new Thread(new ThreadStart(ThreadMethod1));  //thread for Method2
            thread1.Start();//Start Thread
            Console.ReadKey(); //For holding Screen
        }
    }

}

Explanation:
The only change is  the thread.Join() method. Now the second thread would not execute until first thread terminates.
Output:

Now after every compilation you would get this result.

2.Foreground and Background  Thread
Foreground thread keeps your application alive.Once foreground thread ends ,application ends and background threads also terminates.By default ,all threads are foreground.

Note:
To make a thread background just  make the isBackground property true. If this property is false of a thread, it means it is foreground.
e.g 
Thread t=new Thread();
t.IsBackground = true;

Code:
 class Program
    {
        public static void ThreadMethod()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Foreground Thread");
                Thread.Sleep(300);
            }
            Console.WriteLine("Foreground thread is terminated");
        }
        public static  void ThreadMethod1()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Background thread");
                Thread.Sleep(200);
            }
        }




        static void Main(string[] args)
        {
            Thread thread = new Thread(new ThreadStart(ThreadMethod));
            thread.Start();
            thread.Join();       
            Thread thread1 = new Thread(new ThreadStart(ThreadMethod1));
            thread1.IsBackground = true;     //to make a thread background
            thread1.Start();

        }
    }
Out Put:
Foreground thread will execute ten times and application will terminated.
Explanation:
 As I have used Join method ,with which foreground thread will execute until it terminate. As second thread is background and background thread executes till the execution of foreground thread.As foreground thread terminates ,background thread also terminates .So when the above code executes,the first thread will executes ten times and then application will end.
Note:
In Console Application ,if use Console.ReadKey() for screen .The background thread will also execute ten times because Console.Readkey() behave as foreground thread.And we know that background thread executes until foreground thread is alive.

3. ParameterizedThreadStart
We can pass data through start method of a thread. For this purpose ,we use ParameterizedThreadStart delegate instead of  simple ThreadStart  delegate.

Example:
class Program
    {
        public static void ThreadMethod(object o)
        {
            for (int i = 0; i <(int)o; i++)
            {
                Console.WriteLine("thread Proc{0}", i);
            }
            
        }
        static void Main(string[] args)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(ThreadMethod));
            thread.Start(5);
            Console.ReadKey();
        }
    }
OutPut:
thread proc0
thread proc1
thread proc2
thread proc3
thread proc4
Explanation:
In that case ,value is passed to threadMethod as object,You can cast it to any other type.















No comments:

Post a Comment