Share via


Create multiple threads and wait all of them to complete

Question

Tuesday, April 14, 2015 12:02 PM

i got a code for Create multiple threads and wait all of them to complete. they use thread.join() what thread.join() does ?

i guess if i write

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

it means probably when thread1 will finish then thread2 will start and when thread2 will finish then thread3 will start....how join() function will help to start all 3 thread palallel ? please see the below code and help me. thanks

static void Main(string[] args)
{
    Thread t1 = new Thread(doStuff);
    t1.Start();
    Thread t2 = new Thread(doStuff);
    t2.Start();
    Thread t3 = new Thread(doStuff);
    t3.Start();
    t1.Join();
    t2.Join();
    t3.Join();
    Console.WriteLine("All threads complete");
}

static void doStuff()
    {
        //do stuff here
    }

All replies (5)

Tuesday, April 14, 2015 1:16 PM ✅Answered

The Join method blocks the calling thread until the thread that you call the Join() method has terminated: https://msdn.microsoft.com/en-us/library/system.threading.thread.join%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

So in your sample code the thread on which the Main method is executed will be blocked until first t1 and then t2 and t3 has terminated:

static void Main(string[] args)
 {
     Thread t1 = new Thread(doStuff);
     t1.Start();
     Thread t2 = new Thread(doStuff);
     t2.Start();
     Thread t3 = new Thread(doStuff);
     t3.Start();
     t1.Join(); //wait here until t1 has terminated...
     t2.Join(); //wait here until t2 has terminated...
     t3.Join(); //wait here until t3 has terminated...
     Console.WriteLine("All threads complete");
 }

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't post several questions in the same thread.


Tuesday, April 14, 2015 7:06 PM ✅Answered

Yes, both t2 and t3 will continue to run and they will not by affected by the main thread's call to the Join() method of t1.

t1 will also continue until it is done, it is only the main thread that will be blocked.

Please remember to mark all helpful posts that answers your questions as answer and please start a new thread if you have a new question.


Tuesday, April 14, 2015 12:08 PM

The code does what you need. You start all threads. Then you wait for the end of all of them. The order of the Join calls does not matter because all have exitted in the end.

Armin


Tuesday, April 14, 2015 6:18 PM

thanks for answer. when t1.Join(); will execute then t2 & t3 will run in background or not ?


Thursday, April 16, 2015 7:26 AM

this way also problem can be solved.

using System.Threading.Tasks;
using System.Threading;

    class Program
    {
        static void Main(string[] args)
        {
            Task task1 = Task.Factory.StartNew(() => doStuff("Task1"));
            Task task2 = Task.Factory.StartNew(() => doStuff("Task2"));
            Task task3 = Task.Factory.StartNew(() => doStuff("Task3"));
            Task.WaitAll(task1, task2, task3);

            Console.WriteLine("All threads complete");
            Console.ReadLine();
        }

        static void doStuff(string strName)
        {
            for (int i = 1; i <= 3; i++)
            {
                Console.WriteLine(strName + " " + i.ToString());
                Thread.Yield();
            }
        }
    }