Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreading: When would I use a Join?

I see online that it says I use myThread.Join(); when I want to block my thread until another thread finishes. (One of the things I don't get about this is what if I have multiple threads).

But generally, I just don't get when I'd use .Join() or a condition that it's useful for. Can anyone please explain this to me like I'm a fourth grader? Very simple explanation to understand will get my answer vote.

like image 325
Shai UI Avatar asked Dec 19 '10 23:12

Shai UI


People also ask

When should you join a thread?

Join is used only when one thread must wait for the open to finish (lets say thread A prepares a file and thread B cannot continue until the file is ready). There are instance where threads are independent and no join is needed (for example most of daemon threads).

Why should you use the Join () method in your program?

Join method in Java allows one thread to wait until another thread completes its execution. In simpler words, it means it waits for the other thread to die. It has a void type and throws InterruptedException.

Is thread join necessary?

No join or System. exit necessary. Each thread lives its own life. As long as at least one thread is running, the program keeps running.

What is thread join () in threading?

join() is holding up the main thread. All three threads complete before the t1. join() finishes and the main thread moves on to execute the print then t2.


2 Answers

Let's say you want to start some worker threads to perform some kind of calculation, and then do something afterwards with all the results.

List<Thread> workerThreads = new List<Thread>(); List<int> results = new List<int>();  for (int i = 0; i < 5; i++) {     Thread thread = new Thread(() => {         Thread.Sleep(new Random().Next(1000, 5000));         lock (results) {             results.Add(new Random().Next(1, 10));         }     });     workerThreads.Add(thread);     thread.Start(); }  // Wait for all the threads to finish so that the results list is populated. // If a thread is already finished when Join is called, Join will return immediately. foreach (Thread thread in workerThreads) {     thread.Join(); }  Debug.WriteLine("Sum of results: " + results.Sum()); 

Oh yeah, and don't use Random like that, I was just trying to write a minimal, easily understandable example. It ends up not really being random if you create new Random instances too close in time, since the seed is based on the clock.

like image 117
J.D. Avatar answered Sep 25 '22 14:09

J.D.


In the following code snippet, the main thread calls Join() which causes it to wait for all spawned threads to finish:

static void Main() {     Thread regularThread = new Thread(ThreadMethod);     regularThread.Start();      Thread regularThread2 = new Thread(ThreadMethod2);     regularThread2.Start();      // Wait for spawned threads to end.     regularThread.Join();     Console.WriteLine("regularThread returned.");      regularThread2.Join();     Console.WriteLine("regularThread2 returned."); } 

Note that if you also spun up a thread from the thread pool (using QueueUserWorkItem for instance), Join would not wait for that background thread. You would need to implement some other mechanism such as using an AutoResetEvent.

For an excellent introduction to threading, I recommend reading Joe Albahari's free Threading in C#

like image 30
Mitch Wheat Avatar answered Sep 25 '22 14:09

Mitch Wheat