C# program to demonstrate two threads working concurrently
Program.cs
Thread t1 = new Thread(new ThreadStart(ParallelThread.thread1));
Thread t2 = new Thread(new ThreadStart(ParallelThread.thread2));

t1.Start();
t2.Start();

class ParallelThread
{
    public static void thread1()
    {
        for (int i = 100; i <= 105; i++)
        {
            Console.Write(i + " ");
            Thread.Sleep(1500);
        }
    }
    public static void thread2()
    {
        for (int i = 200; i <= 205; i++)
        {
            Console.Write(i + " ");
            Thread.Sleep(1000);
        }
    }
}
Explanation:
In this program, we have created the two static methods: thread1() and thread2() in the ParallelThread class. In the main method, we have created the object of Thread class and passed the thread1 and thread2 methods in ThreadStart delegate with classname.methodname. Then, we used Thread.Sleep() method to suspends the current thread for the specified amount of time. At the end, we called Start() method of Thread class with t1 and t2 objects accordingly. The output clearly shows the two thread worked simultaneously.
Output
kodingwindow@kw:~/csharp$ dotnet run
100 200 201 101 202 102 203 204 103 205 104 105
kodingwindow@kw:~/csharp$ 
Advertisement