Thread ordering

There is no guarantee that which thread will execute first. In the following program, any thread can execute first depending on how operating system schedule

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ThreadPrograms
{
    class Program
    {
        static void Main(string[] args)
        {

            var t1 = new Thread(MyThread1);
            var t2 = new Thread(MyThread2);
            t1.Start();
            t2.Start();
            Console.WriteLine("This is main thread");
        }

        static void MyThread1()
        {
            Console.WriteLine("Running in a thread 1");
        }

        static void MyThread2()
        {
            Console.WriteLine("Running in a thread 2");
        }
    }
}

 

Tags:

Add a Comment

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