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, 12:58 PM
In last example I see no multithreading. It's poor example or I don't understand something?
Prakash TripathiPosted Apr 21, 2016, 6:48 AM
Thnx Gowtham.
Gowtham RajamanickamPosted Apr 21, 2016, 6:39 AM
very good
Prakash TripathiPosted Mar 6, 2016, 10:33 PM
Thnx Kumaresh.
Prakash TripathiPosted Mar 6, 2016, 10:33 PM
Thnx Karthiga.
Kumaresh RajalingamPosted Mar 6, 2016, 8:27 PM
good one
Kumaresh RajalingamPosted Mar 6, 2016, 8:27 PM
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 8:15 PM
god one
Sr KarthigaPosted Mar 6, 2016, 8:15 PM
Nice explanation
Humayun Kabir MamunPosted Dec 7, 2015, 4:26 AM
Nice...
Gowtham RajamanickamPosted Aug 27, 2015, 9:52 AM
good
Gopi ChandPosted Aug 25, 2015, 10:16 AM
Well done
Vaikesh K PPosted Aug 25, 2015, 1:27 AM
Good One
Gowtham KPosted Aug 24, 2015, 11:07 PM
Good one
Sumit JoshiPosted Aug 24, 2015, 9:55 AM
thanks for sharing..
Sibeesh VenuPosted Aug 24, 2015, 9:22 AM
Nice Share :)
Karthikeyan KPosted Aug 24, 2015, 9:10 AM
Good one sir...Thanks for share
RakeshPosted Aug 24, 2015, 7:01 AM
Good one
Mohammed IbrahimPosted Aug 24, 2015, 6:36 AM
nice
Rajeesh MenothPosted Aug 24, 2015, 6:36 AM
Nice One