Tuesday 21 August 2018

C#: Thread join

Suppose you divided a big problem into small tasks, let’s say one big problem is divided into 5 small tasks. So to solve the whole problem. All the 5 tasks must be completed, one task may finishes immediately, other takes 5 minutes, and other may take 1 hour etc., Your program must wait until all the tasks finishes its execution. Here the join method comes into picture.

The join() method of Thread can be used to cause the current thread to block until the specified thread finish its execution.
  
HelloWorld.cs
using System.Threading;
using System;

namespace ThreadingTutorial
{
    class HelloWorld
    {
        static void Main(string[] args)
        {
            Thread t1 = new Thread(() => task(1000));
            Thread t2 = new Thread(() => task(4000));
            Thread t3 = new Thread(() => task(2000));
            Thread t4 = new Thread(() => task(5000));
            Thread t5 = new Thread(() => task(3000));

            t1.Name = "Thread1";
            t2.Name = "Thread2";
            t3.Name = "Thread3";
            t4.Name = "Thread4";
            t5.Name = "Thread5";

            t1.Start();
            t2.Start();
            t3.Start();
            t4.Start();
            t5.Start();

            t1.Join();
            t2.Join();
            t3.Join();
            t4.Join();
            t5.Join();

            Console.WriteLine("All threads Finished Execution");
        }

        public static void task(int sleepTime)
        {
            Console.WriteLine(Thread.CurrentThread.Name + " : starts execution");
            Thread.Sleep(sleepTime);
            Console.WriteLine(Thread.CurrentThread.Name + " : finishes execution");
        }
    }
}

Output

Thread1 : starts execution
Thread2 : starts execution
Thread3 : starts execution
Thread5 : starts execution
Thread4 : starts execution
Thread1 : finishes execution
Thread3 : finishes execution
Thread5 : finishes execution
Thread2 : finishes execution
Thread4 : finishes execution
All threads Finished Execution

To understand join method better, comment all the join() calls and re-run the application.


HelloWorld.cs

using System.Threading;
using System;

namespace ThreadingTutorial
{
    class HelloWorld
    {
        static void Main(string[] args)
        {
            Thread t1 = new Thread(() => task(1000));
            Thread t2 = new Thread(() => task(4000));
            Thread t3 = new Thread(() => task(2000));
            Thread t4 = new Thread(() => task(5000));
            Thread t5 = new Thread(() => task(3000));

            t1.Name = "Thread1";
            t2.Name = "Thread2";
            t3.Name = "Thread3";
            t4.Name = "Thread4";
            t5.Name = "Thread5";

            t1.Start();
            t2.Start();
            t3.Start();
            t4.Start();
            t5.Start();

            /*t1.Join();
            t2.Join();
            t3.Join();
            t4.Join();
            t5.Join();*/

            Console.WriteLine("All threads Finished Execution");
        }

        public static void task(int sleepTime)
        {
            Console.WriteLine(Thread.CurrentThread.Name + " : starts execution");
            Thread.Sleep(sleepTime);
            Console.WriteLine(Thread.CurrentThread.Name + " : finishes execution");
        }
    }
}

Output

Thread1 : starts execution
Thread3 : starts execution
Thread2 : starts execution
Thread5 : starts execution
Thread4 : starts execution
All threads Finished Execution
Thread1 : finishes execution
Thread3 : finishes execution
Thread5 : finishes execution
Thread2 : finishes execution
Thread4 : finishes execution


As you see the output, the message 'All threads Finished Execution' printed before all the threads finishes their execution.

'Join' method also available in overloaded forms. Methods that take arguments blocks the calling thread until the thread represented by this instance terminates or the specified time elapse

Join()
Join(Int32)
Join(TimeSpan)


Previous                                                 Next                                                 Home

No comments:

Post a Comment