Multithreading: when will I use Join?

I see online that he says that I am using myThread.Join(); when I want to block a thread until another thread ends. (One of the things I don't understand about is that if I have multiple threads).

But actually, I just don’t understand when I will use .Join() or the condition for which this is useful. Can someone explain this to me as if I are fourth grade? A very simple explanation to understand will get my answer.

+43
multithreading c #
Dec 19 '10 at 23:35
source share
6 answers

Suppose you want to start some workflows to perform some kind of calculation, and then do something later 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 yes, and I don't use Random, I was just trying to write a minimal, easy to understand example. This ends up being non-random if you create new random instances too close in time, since the seed is based on the clock.

+49
Dec 20 '10 at 0:26
source share

In the following code snippet, the main thread calls Join () , which is why it waits for all generated threads to complete:

 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 create a thread from a thread pool (for example, using QueueUserWorkItem), Join will not wait for this background thread. You will need to implement another mechanism, for example, using AutoResetEvent.

For a great introduction to threads, I recommend reading Joe Albahari for free Threading in C #

+15
Dec 19 '10 at 23:47
source share

A connection is mainly used when you need to wait for a thread (or its group) to complete before your code is processed.

For this reason, it is also especially useful when you need to collect the result of a thread.

According to Arafangion's comment below, it is also important to join streams if you need to do a cleanup / housekeeping code after creating the stream.

+8
Dec 19 '10 at 23:43
source share

This is a very simple program to demonstrate the use of Thread Join . Please follow my comments for a better understanding. Record this program as is.

  using System; using System.Threading; namespace ThreadSample { class Program { static Thread thread1, thread2; static int sum=0; static void Main(string[] args) { start(); Console.ReadKey(); } private static void Sample() { sum = sum + 1; } private static void Sample2() { sum = sum + 10; } private static void start() { thread1 = new Thread(new ThreadStart(Sample)); thread2 = new Thread(new ThreadStart(Sample2)); thread1.Start(); thread2.Start(); // thread1.Join(); // thread2.Join(); Console.WriteLine(sum); Console.WriteLine(); } } } 

1. The first time starts as is (with comments): Then the result will be 0 (the initial value) or 1 (at the end of stream 1) or 10 (or the end of the stream)

2.Run with deleting the thread1.Join() comment: The result should always be more than thread1.Join() fired, and thread 1 must be finished before receiving the amount.

3.Run with deleting all comments: The result should always be 11

+5
Oct 23 '14 at 19:39
source share

Adding a 300 ms delay in the Sample method and a 400 ms delay in Sample2 from the devopsEMK message will make understanding easier.

So you may notice that by removing the comment from "thread1.Join ();" line, the main thread waits for the completion of "thread1" and only after moving it.

0
Oct. 23 '15 at 14:32
source share

Another example, when your workflow allows you to talk, reads from the input stream, while the read method can work forever, and you want to somehow avoid this - by applying a timeout using another watchdog:

 // worker thread var worker = new Thread(() => { Trace.WriteLine("Reading from stream"); // here is the critical area of thread, where the real stuff happens // Sleep is just an example, simulating any real operation Thread.Sleep(10000); Trace.WriteLine("Reading finished"); }) { Name = "Worker" }; Trace.WriteLine("Starting worker thread..."); worker.Start(); // watchdog thread ThreadPool.QueueUserWorkItem((o) => { var timeOut = 5000; if (!worker.Join(timeOut)) { Trace.WriteLine("Killing worker thread after " + timeOut + " milliseconds!"); worker.Abort(); } }); 
0
Sep 13 '17 at 14:21
source share



All Articles