Monitor
Monitor

Reputation: 352

Observing Different outputs in Threads

I am exploring threading concept, Every time when i tried to execute the below code, I am getting different output. I am starting ThreadOne first, But why is ThreadTwo getting started. Why am I getting Different output? Can someone explain in Detail? Thanks In Advance.

static Object newLockobj = new Object();
    static void Main(string[] args)
    {
        Thread tobj1 = new Thread(ThreadOne);
        Thread tobj2 = new Thread(ThreadTwo);
        tobj1.Start();
        tobj2.Start();

        Console.ReadLine();
    }

    static void ThreadOne()
    {
        Console.WriteLine("Thread One Entered");
        lock (newLockobj)
        {
            Console.WriteLine("Thread 1 started");
            Thread.Sleep(2000);
            Console.WriteLine("Thread 1 ended");
        }
    }

    static void ThreadTwo()
    {
        Console.WriteLine("Thread Two Entered");
        lock (newLockobj)
        {
            Console.WriteLine("Thread 2 started");
            Thread.Sleep(1000);
            Console.WriteLine("Thread 2 ended");
        }
    }

Observed Output

Upvotes: 0

Views: 150

Answers (2)

JTMon
JTMon

Reputation: 3199

This is in the heart of threads programming. While you have control over which thread you want to start first, the CPU is not obliged to follow your order of events. During the execution of one thread the CPU is guaranteed to follow you order of code execution as per your programming. When it comes to scheduling different threads, the CPU will follow its own logic and sometimes that logic can seem like random behavior. It will allocate processing time to different threads as it sees fit.

I suggest you read up more on threads programming as not fully understanding this concept and appreciating it's potential pitfalls will lead to many days of hair pulling and possible deadlocks :)

Just google: c# threading. You will get plenty of good resources!

Upvotes: 1

WeSt
WeSt

Reputation: 2684

This is a basic examle for Concurrency. Once you call the .Start() method on a thread object, it starts getting executed independent of the thread that started it. What your programm is doing:

  1. Create the thread objects
  2. Start tobj1 (the thread is not executed directly, but once the scheduler finds a place to put it)
  3. Start tobj2 (the thread is again not executed directly)

The scheduler is responsible to tell the processor which process and which thread to execute. The order in which the threads are started is of no importance.

As soon as one thread starts, he prints the first line ("Thread X Entered"). The Lock is a synchronization statement. Since both threads sync on the same object instance, they cannot enter the Lock block both (Mutual Exclusion). The Lock statement only ensures that the process can work in the critical section without the other process working it his critical section. As you see in your right output, a thread can be interrupted although he is in his critical section ("Thread 2 started", "Thread One Entered", "..."). "Thread One", however, is not allowed to his critical section since the other thread did not release the lock yet.

Upvotes: 3

Related Questions