Jeevan Bhatt
Jeevan Bhatt

Reputation: 6101

Internally how does a thread work?

Thread t = new Thread (WriteY);
t.Start();            
for (int i = 0; i < 1000; i++) Console.Write ("x");

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

internally How does thread work ? means why output of above code is not fix every time i run, sequence of 'x' and 'y' is different?

Upvotes: 0

Views: 1326

Answers (1)

Will Hartung
Will Hartung

Reputation: 118651

All multitasking systems have a scheduler. A scheduler decides what unit of work will execute next. A basic scheduler can be something that runs of a high resolution timer (like, say, every 100ms, a task switch happens). Clearly, modern implementations are much more complicated than that.

That said, most modern threading implementations rely on a scheduler within the kernel. Many of these schedulers are NOT deterministic. That is, there is no guarantee that a context switch (i.e. a switch between runnable instances managed by the scheduler) will happen at any specific time.

What you are seeing are the discrepancies in that scheduler for your system.

Upvotes: 5

Related Questions