TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Thread Join Method (Join Array of Threads)

Use the Join method on an array of threads to wait for all threads to finish.
Join. On the Thread type, we find the Join instance method. It enables us to wait until the thread finishes. We use the Join method on an array of threads to implement useful threading functionality.Threads
Example. This program creates an array of 4 threads and then calls a method on each thread. The method called (Start) requires 10,000 milliseconds to complete because it calls Thread.Sleep.

Next: We loop again through the threads, which have now been started, and call Join on each of them.

And: The Join method causes the execution to stop until that thread terminates.

Therefore: When the loop where we call Join completes, all of the threads have completed.

Sleep
C# program that calls Join method on Thread using System; using System.Diagnostics; using System.Threading; class Program { static void Main() { var stopwatch = Stopwatch.StartNew(); // Create an array of Thread references. Thread[] array = new Thread[4]; for (int i = 0; i < array.Length; i++) { // Start the thread with a ThreadStart. array[i] = new Thread(new ThreadStart(Start)); array[i].Start(); } // Join all the threads. for (int i = 0; i < array.Length; i++) { array[i].Join(); } Console.WriteLine("DONE: {0}", stopwatch.ElapsedMilliseconds); } static void Start() { // This method takes ten seconds to terminate. Thread.Sleep(10000); } } Output DONE: 10001
The results of this program demonstrate the effectiveness of the threading and the Join method. The four threads would have slept for ten seconds each, but the entire program only took ten seconds total to execute.
ThreadPool. Instead of using a Thread array and implementing the threading functionality by yourself, you can use the ThreadPool type. This has further improvements that handle threads that finish before other threads, probably improving performance.ThreadPool
Summary. Most threading libraries provide a Join procedure, and the .NET System.Threading namespace functionality is no different. This method provides blocking functionality that waits for the specified thread to complete.

And: You do not need to use Join in an array or List, but it is often desirable to.

Tip: With parallel execution, you can make many programs faster and the Join method serves an essential role in this goal.

ThreadStart
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf