Xaisoft
Xaisoft

Reputation: 46591

Explain the thread execution order here?

From the code below, I though thhat "y" would be printed out before x, but "x" starts printing first. I am reading this in a linear fashion, so why does "x" start printing first even though t.Start() is called first in the main mehod?

static void Main()
{

    Thread t = new Thread(ThreadTest.WriteY);
    t.Start();

    for(int i = 0; i < 1000; i++) Console.Write("x");
}

public class ThreadTest
{
    public static void WriteY()
    {
    for(int i = 0; i < 1000; i++) Console.WriteLine("y");
    }
}

Upvotes: 3

Views: 858

Answers (6)

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

Starting a new thread may require quite some time, so it will take a while before WriteY gets to run. In the meantime the main thread will continue to run and thus you will see a lot of xs printed in most if not all cases.

Furthermore the scheduling itself is handled by the OS.

In short: You shouldn't try to guess the order of execution based on reading the source code.

Upvotes: 3

anon
anon

Reputation:

Because it isn't assured what starts first. There isn't a deterministic order in which threads are executed. If you start your program n times, it can be assumed that you see both x and y printed first.

If you want to ensure an order in the way threads are executed you should look at Thread.Join

Upvotes: 1

DarkSquirrel42
DarkSquirrel42

Reputation: 10257

thread execution order is unpredictable

in this case it will probably be the code that builds the thread (stackframe/etc) that is slowing things down

Upvotes: 1

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391336

Well, consider it this way.

Give your friend a stack of papers, and keep a stack yourself.

Then you tell your friend "start writing out numbers on those papers", and then immediately you start doing the same.

Now tell me, who of you will finish first?

And, considering this is your question, why?

It is impossible to deterministically type out a correct answer here because there are so many things impacting how fast you two can:

  • start writing out numbers
  • write out numbers
  • move on to the next piece of paper

So basically, your friend might end up starting before you manage to start, or vice versa, but you cannot know beforehand, and it isn't "the right answer".

Every time you run this program, it has the chance of behaving differently, at least in terms of execution order between the two threads.

Having said that, there is overhead involved in spinning up a new thread, which might tip the scale in favor of the main thread entering the loop before the extra thread does. However, I'd be surprised if there is no way that the opposite might happen.

Upvotes: 11

Justin Niessner
Justin Niessner

Reputation: 245419

Just because you start a thread at a certain time doesn't mean that the code in that thread is going to start before the execution of the next line of code in the calling method.

I would guess that you could run the program thousands of times and wind up with some that start with 'y' and others that start with 'x'.

Upvotes: 2

BFree
BFree

Reputation: 103740

When you spawn off a new thread, the Operating System takes over and schedules the work to happen at a later time. The point is that you have no way to know when it'll take place, which is why you an run the same app multiple times and see different results every time.

Upvotes: 3

Related Questions