-2
System.Threading.Thread thd1 = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
thd1.Start();
System.Threading.Thread thd2 = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
thd2.Start();
thd1.Join();
thd2.Join();

i just like to know why people use Thread.Join() ? Thread.Join() block next line to execute and wait for a specific thread to complete. when that specific thread is completed then next line gets executed.

just tell me the intention of Thread.Join() with right example. thanks

Mou
  • 15,673
  • 43
  • 156
  • 275

2 Answers2

1

it's one way to order the thread execution if you need t1 before t2 look at the following example :

static void Main() 
{
    Thread t1 = new Thread(() => 
    { 
        Thread.Sleep(4000);
        Console.WriteLine("t1 is ending.");
    });
    t1.Start();

    Thread t2 = new Thread(() => 
    { 
        Thread.Sleep(1000);
        Console.WriteLine("t2 is ending.");
    });
    t2.Start();

    t1.Join();
    Console.WriteLine("t1.Join() returned.");

    t2.Join();
    Console.WriteLine("t2.Join() returned.");
}

This example produces the following output:

t2 is ending. t1 is ending. t1.Join() returned. t2.Join() returned.

1

Thread.Join() block next line to execute and wait for a specific thread to complete.

And that's precisely why people use Thread.Join().

Yes, you want your different threads doing their different things separately as much as possible, but when Thread A needs something that Thread B is obtaining, or can't do something until Thread B has done something, then the easiest way to get to that point is to use Thread.Join().

That's not terribly useful in the example in your question, because there you have three threads one of which is doing nothing useful.

A slightly more realistic version would be:

Thread thd1 = new System.Threading.Thread(new ThreadStart(DoWork));
thd1.Start();
DoWork();
thd1.Join();

Here you've two threads doing two threads-worth of work. By the time we get to thd1.Join() it's likely that thd1 has already completed and joining is a nop, but just in case we wait to make sure it's finished.

Another more realistic example is when you create several threads and then join those several threads because while it's wasteful to have the creating thread just waiting, it's simpler to code a loop that sets up X threads than a loop that sets up X-1 threads and does the work of that final item.

Avoiding Joining as long as possible (or at all) is certainly a good idea that will often give you greater efficient use of available threads, but Joining is simple, and simple is good too.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • the sample code you gave not very clear your intention because u run DoWork() from thread and also calling DoWork() by main thread. still thread.join() intention is not very clear. looking for bit realistic example. thanks – Mou May 25 '15 at 10:35
  • I run `DoWork` twice because that's what the example in the original question was doing, but taking three threads to do. – Jon Hanna May 25 '15 at 11:24
  • sorry to say but your example was not right one to explain the right usage of join function. if possible please come with another example. thanks – Mou May 25 '15 at 14:37