C# Thread Class

Introduction

The Thread class in C# provides a way to create and manage threads. It is part of the System.Threading namespace and allows you to perform concurrent operations within your applications. By using the Thread class, you can run multiple operations simultaneously, improving the performance and responsiveness of your applications.

Key Features of the Thread Class

  • Thread Creation: Allows you to create new threads.
  • Thread Management: Provides methods to start, pause, resume, and stop threads.
  • Thread Synchronization: Supports thread synchronization mechanisms.
  • Thread Priority: Allows you to set the priority of a thread.
  • Thread States: Provides properties to check the state of a thread.

Creating and Starting a Thread

Example

using System;
using System.Threading;

namespace ThreadExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creating a new thread
            Thread thread = new Thread(DoWork);

            // Starting the thread
            thread.Start();

            // Waiting for the thread to finish
            thread.Join();

            Console.WriteLine("Thread has completed.");
        }

        static void DoWork()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine($"Thread is working... {i}");
                Thread.Sleep(1000); // Simulate work
            }
        }
    }
}

Output

Thread is working... 0
Thread is working... 1
Thread is working... 2
Thread is working... 3
Thread is working... 4
Thread has completed.

Thread States

The Thread class has various states that indicate the current status of a thread:

  • Unstarted: The thread has been created but not yet started.
  • Running: The thread is running.
  • Blocked: The thread is blocked, waiting for a resource.
  • Waiting: The thread is waiting for another thread to perform a task.
  • Terminated: The thread has finished executing.

Example

using System;
using System.Threading;

namespace ThreadStateExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread thread = new Thread(DoWork);
            Console.WriteLine("Thread State: " + thread.ThreadState); // Unstarted

            thread.Start();
            Console.WriteLine("Thread State after Start(): " + thread.ThreadState); // Running

            thread.Join();
            Console.WriteLine("Thread State after Join(): " + thread.ThreadState); // Terminated
        }

        static void DoWork()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine($"Thread is working... {i}");
                Thread.Sleep(1000); // Simulate work
            }
        }
    }
}

Output

Thread State: Unstarted
Thread State after Start(): Running
Thread is working... 0
Thread is working... 1
Thread is working... 2
Thread is working... 3
Thread is working... 4
Thread State after Join(): Stopped

Setting Thread Priority

You can set the priority of a thread using the ThreadPriority enumeration. This helps in managing the execution order of multiple threads.

Example

using System;
using System.Threading;

namespace ThreadPriorityExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread highPriorityThread = new Thread(DoWork) { Priority = ThreadPriority.Highest };
            Thread lowPriorityThread = new Thread(DoWork) { Priority = ThreadPriority.Lowest };

            highPriorityThread.Start();
            lowPriorityThread.Start();

            highPriorityThread.Join();
            lowPriorityThread.Join();

            Console.WriteLine("Both threads have completed.");
        }

        static void DoWork()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine($"{Thread.CurrentThread.Priority} priority thread is working... {i}");
                Thread.Sleep(500); // Simulate work
            }
        }
    }
}

Output

Highest priority thread is working... 0
Highest priority thread is working... 1
Highest priority thread is working... 2
Highest priority thread is working... 3
Highest priority thread is working... 4
Lowest priority thread is working... 0
Lowest priority thread is working... 1
Lowest priority thread is working... 2
Lowest priority thread is working... 3
Lowest priority thread is working... 4
Both threads have completed.

Suspending and Resuming Threads

Note: The Thread.Suspend and Thread.Resume methods are deprecated due to potential deadlocks and other synchronization problems. It is recommended to use other synchronization techniques like Monitor, Mutex, Semaphore, etc.

Thread Synchronization

Using lock Statement

The lock statement ensures that a block of code runs to completion without being interrupted by other threads.

Example

using System;
using System.Threading;

namespace ThreadSynchronizationExample
{
    class Program
    {
        private static readonly object _lock = new object();
        private static int _counter = 0;

        static void Main(string[] args)
        {
            Thread thread1 = new Thread(IncrementCounter);
            Thread thread2 = new Thread(IncrementCounter);

            thread1.Start();
            thread2.Start();

            thread1.Join();
            thread2.Join();

            Console.WriteLine($"Counter value: {_counter}");
        }

        static void IncrementCounter()
        {
            for (int i = 0; i < 1000; i++)
            {
                lock (_lock)
                {
                    _counter++;
                }
            }
        }
    }
}

Output

Counter value: 2000

Using Thread.Join

The Join method is used to block the calling thread until the thread on which Join is called terminates.

Example

using System;
using System.Threading;

namespace ThreadJoinExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread thread = new Thread(DoWork);
            thread.Start();

            // Block the calling thread until the thread on which Join is called terminates.
            thread.Join();

            Console.WriteLine("Thread has completed.");
        }

        static void DoWork()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine($"Thread is working... {i}");
                Thread.Sleep(1000); // Simulate work
            }
        }
    }
}

Output

Thread is working... 0
Thread is working... 1
Thread is working... 2
Thread is working... 3
Thread is working... 4
Thread has completed.

Conclusion

The Thread class in C# provides powerful features for creating and managing threads. Understanding the thread life cycle, states, and synchronization techniques is crucial for writing effective multithreaded applications. By leveraging these features, you can improve the performance and responsiveness of your applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top