C# provides built-in support for multithreaded programming. A multi-threaded program contains two or more parts that can run concurrently. Each part of such a program is called an thread, and each thread defines a separate path of execution.
When a C# program starts up, one thread begins running immediately. This is usually called the main thread of our program.
- It is the thread under which other “child” threads will be created.
- Often, it must be the last thread to finish execution because it performs various shutdown actions.
Welcome to the Main thread Welcome to the Child thread Welcome to the Child thread
Explanation: The above program consists of only one thread that is known as the main thread. The main thread works just like other thread but it starts automatically, you need not to require any
Start() method to start the execution of the main thread. Both thrA and thrB are the child thread of main thread. First main thread starts its working after that child thread starts their working.
For accessing main thread you require the Thread class object to refer it. You can create this by using the CurrentThread property of the Thread class. It will return the reference to the thread in which it used. So when you use CurrentThread property inside the main thread you will get the reference of the main thread. After that, you will get control over the main thread just like another thread.
Main thread does not have name The priority of main thread is: Normal The name of main thread is: Main Thread
We can create a deadlock by just using Main thread, i.e. by just using a single thread. The following C# program demonstrates this.
Enter into DEADLOCK!!
Max real time limit exceeded due to either by heavy load on server or by using sleep function
Explanation: The statement “
Thread.currentThread().join()“, will tell the Main thread to wait for this thread(i.e. wait for itself) to die. Thus the Main thread waits for itself to die, which is nothing but a deadlock.
- Naming a thread and fetching name of current thread in C#
- How to check whether a thread is a background thread or not in C#
- C# | Check if a thread belongs to managed thread pool or not
- Main Method in C#
- C# | Creating a synchronized (thread-safe) wrapper for a SortedList object
- C# | Check if StringCollection is synchronized (thread safe)
- Thread.CurrentThread Property in C#
- C# | Check if the BitArray is synchronized (thread safe)
- C# | Check if StringDictionary is synchronized (thread safe)
- C# | Check if ListDictionary is synchronized (thread safe)
- C# | Check if HybridDictionary is Synchronized (thread safe)
- C# | Check if Hashtable is synchronized (thread safe)
- C# | Check if ArrayList is Synchronized (thread safe)
- C# | Check if an array is synchronized (thread safe) or not
- C# | Creating a synchronized (thread-safe) wrapper for the Hashtable
- C# | Creating a synchronized (thread-safe) wrapper for the ArrayList
- C# | Thread Priority in Multithreading
- Lifecycle and States of a Thread in C#
- How to Terminate a Thread in C#
- Suspending the current thread for the specified amount of time in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai