I am here again to continue the discussion of Threading. Today we will explain how to create threads and related concepts.
In case you haven’t had a look at our previous articles, you can go through the following article series:
- Threading Simplified: Part 1 (Threads Inception)
- Threading Simplified: Part 2 (Multithreading Concepts)
- Threading Simplified: Part 3 (Threads type)
Threads can be created in several ways. Let’s go through them one by one in the following.
- Using only with Thread class
This is the simplest way to create threads, it doesn’t use any delegate and simply keep reference to the method it must call.
Output- Thread t1 = new Thread(PrintMe1);
- t1.Start();
- static void PrintMe1()
- {
- Console.WriteLine("I am created using Thread Class");
- }

- Using ThreadStart delegate
The ThreadStart delegate works similar to Step 1 above. It enables starting a thread without passing any argument to the target method.
Output- Thread t2 = new Thread(new ThreadStart(PrintMe2));
- t2.Start();
- static void PrintMe2()
- {
- Console.WriteLine("I am created using ThreadStart delegate");
- }

- Using ParameterizedThreadStart delegate
The ParameterizedThreadStart delegate starts a thread by passing an argument (of any type) to the target method. The argument should be declared as an object type in the target method.
Output- Thread t3 = new Thread(new ParameterizedThreadStart(PrintMeWithParam));
- t3.Start("ParameterizedThreadStart delegate");
- static void PrintMeWithParam(object param)
- {
- Console.WriteLine("I am created using {0}", param);
- }

- Using anonymous delegate
An Anonymous delegate can be used to invoke a thread. The following example also passes the parameter to the target method.
Output- Thread t4 = new Thread(delegate()
- {
- PrintMeWithParam("anonymous delegate"); //Defined in step-3 above.
- });
- t4.Start();

- Using lambda expression
Lambda expressions can also be used to start a thread. The following example shows how to pass a parameter as well to the target method.
Output- Thread t5 = new Thread(() => PrintMeWithParam("lambda expression"));
- //PrintMeWithParam defined in step-3 above
- t5.Start();

Join Method
The Join method causes a wait for a thread to finish. In a multi-threading scenario, it can be used to provide blocking functionality by allowing waits for the specified thread.
Let’s first understand the problem without using Join.
- Thread t1 = new Thread(PrintMe);
- t1.Name = "Thread-1";
- t1.Start();
- Thread t2 = new Thread(PrintMe);
- t2.Name = "Thread-2";
- t2.Start();

Here you can see that Thread-1 and Thread-2 are competing with each other to execute.
Now let’s run the same code with the join method as in the following snippet.
- Thread t1 = new Thread(PrintMe);
- t1.Name = "Thread-1";
- t1.Start();
- t1.Join();
- Thread t2 = new Thread(PrintMe);
- t2.Name = "Thread-2";
- t2.Start();

Now you can see that Thread-2 executes only when Thread-1 finishes or ends the execution.
I hope you have liked the article, please share your comments/suggestions in the comments section.


Tomasz JanikPosted Apr 17, 2017, 8:58 AM EDT
In last example I see no multithreading. It's poor example or I don't understand something?
Prakash TripathiTOP 100Posted Apr 21, 2016, 2:48 AM EDT
Thnx Gowtham.
Gowtham RajamanickamTOP 100Posted Apr 21, 2016, 2:39 AM EDT
very good
Prakash TripathiTOP 100Posted Mar 6, 2016, 5:33 PM EST
Thnx Kumaresh.
Prakash TripathiTOP 100Posted Mar 6, 2016, 5:33 PM EST
Thnx Karthiga.
Kumaresh RajalingamPosted Mar 6, 2016, 3:27 PM EST
good one
Kumaresh RajalingamPosted Mar 6, 2016, 3:27 PM EST
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 3:15 PM EST
god one
Sr KarthigaPosted Mar 6, 2016, 3:15 PM EST
Nice explanation
Humayun Kabir MamunPosted Dec 6, 2015, 11:26 PM EST
Nice...
Gowtham RajamanickamTOP 100Posted Aug 27, 2015, 5:52 AM EDT
good
Gopi ChandPosted Aug 25, 2015, 6:16 AM EDT
Well done
Vaikesh K PPosted Aug 24, 2015, 9:27 PM EDT
Good One
Gowtham KTOP 100Posted Aug 24, 2015, 7:07 PM EDT
Good one
Sumit JoshiPosted Aug 24, 2015, 5:55 AM EDT
thanks for sharing..
Sibeesh VenuTOP 100Posted Aug 24, 2015, 5:22 AM EDT
Nice Share :)
Karthikeyan KPosted Aug 24, 2015, 5:10 AM EDT
Good one sir...Thanks for share
RakeshPosted Aug 24, 2015, 3:01 AM EDT
Good one
Mohammed IbrahimPosted Aug 24, 2015, 2:36 AM EDT
nice
Rajeesh MenothPosted Aug 24, 2015, 2:36 AM EDT
Nice One