Threading Simplified: Part 4 (Threads Creation)

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:

Threads can be created in several ways. Let’s go through them one by one in the following.

  1. 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.
    1. Thread t1 = new Thread(PrintMe1);  
    2. t1.Start();  
    3.   
    4. static void PrintMe1()  
    5. {  
    6.    Console.WriteLine("I am created using Thread Class");  
    7. }  
    Output

    Thread class

  2. 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.
    1. Thread t2 = new Thread(new ThreadStart(PrintMe2));  
    2. t2.Start();  
    3.   
    4. static void PrintMe2()  
    5. {  
    6.    Console.WriteLine("I am created using ThreadStart delegate");  
    7. }  
    Output

    ThreadStart delegate

  3. 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.
    1. Thread t3 = new Thread(new ParameterizedThreadStart(PrintMeWithParam));  
    2. t3.Start("ParameterizedThreadStart delegate");  
    3.   
    4. static void PrintMeWithParam(object param)  
    5. {  
    6.    Console.WriteLine("I am created using {0}", param);  
    7. }  
    Output

    Using ParameterizedThreadStart delegate

  4. Using anonymous delegate

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

    cmd

  5. 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.
    1. Thread t5 = new Thread(() => PrintMeWithParam("lambda expression"));   
    2. //PrintMeWithParam defined in step-3 above  
    3. t5.Start();  
    Output

    lemda expression

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.

  1. Thread t1 = new Thread(PrintMe);  
  2. t1.Name = "Thread-1";  
  3. t1.Start();  
  4.   
  5. Thread t2 = new Thread(PrintMe);  
  6. t2.Name = "Thread-2";  
  7. t2.Start();  
Output

Join Demo

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.
  1. Thread t1 = new Thread(PrintMe);  
  2. t1.Name = "Thread-1";  
  3. t1.Start();  
  4. t1.Join();  
  5.   
  6. Thread t2 = new Thread(PrintMe);  
  7. t2.Name = "Thread-2";  
  8. t2.Start();  
Output

Output

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.

Up Next
    Ebook Download
    View all
    Learn
    View all