title: Order of thread activation in a c# program on a 4 cores PC
body: <p>I am confused about the thread activation order of the following C# code. It creates 10 threads, starts them randomly, each thread simulates to execute a time-consuming work 10 times, if you check the debug output, the thread seems not to be picked up not randomly, please see below output example, notice thread #3, #5, #6 is always picked up, when #3 #5 #6 is finished, then #10 #2 #8 is always picked up, etc... (I know the design is bad, please focus on the phenomenon)</p>
  
  <p>My PC has i7-7820HQ cpu which has 4 cores, and is running windows 10.</p>
  
  <p>Could someone explain why these threads are not picked randomly and they seemed to be grouped somehow.</p>
  
  <p>Thanks a lot!</p>
  
  <p>---- Debug output ----</p>
  
  <pre>
  Thread #10 acquired the lock and is working for task #0 
  Thread #5 acquired the lock and is working for task #0 
  Thread #3 acquired the lock and is working for task #0
  Thread #6 acquired the lock and is working for task #0
  Thread #5 acquired the lock and is working for task #1
  Thread #3 acquired the lock and is working for task #1
  Thread #6 acquired the lock and is working for task #1
  ...
  Thread #5 acquired the lock and is working for task #9
  Thread #3 acquired the lock and is working for task #9
  Thread #6 acquired the lock and is working for task #9
  ...
  Thread #8 acquired the lock and is working for task #0
  Thread #2 acquired the lock and is working for task #0
  Thread #8 acquired the lock and is working for task #1
  Thread #2 acquired the lock and is working for task #1
  Thread #10 acquired the lock and is working for task #1
  ...
  Thread #8 acquired the lock and is working for task #9
  Thread #2 acquired the lock and is working for task #9
  Thread #10 acquired the lock and is working for task #9
  ...
  </pre>
  
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Text;
  using System.Threading;
  using System.Threading.Tasks;
  
  namespace ConsoleApp1
  {
      class Program
      {
          static Hashtable _sharedBetweenThreads = new Hashtable();
  
          static void Main(string[] args)
          {
              Random random = new Random(DateTime.Now.Second);
              var startOrders = new int[10];
              for (int i = 0; i &lt; startOrders.Length; i++)
              {
                  startOrders[i] = i;
              }
  
              //shuffle the array
              for (int i = startOrders.Length - 1; i &gt;= 0; i--)
              {
                  int j = random.Next(0, i);
                  int temp = startOrders[i];
                  startOrders[i] = startOrders[j];
                  startOrders[j] = temp;
              }
  
  
              Thread[] threads = new Thread[startOrders.Length];
              for(int i = 0; i &lt; startOrders.Length; i++)
              {
                  threads[i] = new Thread(new ThreadStart(ThreadProc));
              }
  
  
              for (int i = 0; i &lt; startOrders.Length; i++)
              {
                  threads[startOrders[i]].Start();
              }
  
              Console.ReadLine();
          }
  
          static void ThreadProc()
          {
              // simulates there are 10 tasks needs to do.
              for (int i = 0; i &lt; 10; i++)
              {
                  lock (_sharedBetweenThreads.SyncRoot)
                  {
                      Debug.Print(string.Format("Thread #{0} acquired the lock and is working for task #{1}", Thread.CurrentThread.ManagedThreadId, i));
  
                      // simulates a work.
                      Thread.Sleep(500);
                  }
  
              }
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/QK30N.jpg" rel="nofollow noreferrer">The screenshot of debug output</a></p>
  
  <p>--- Additional infos ---</p>
  
  <ul>
  <li>The program is not a parallel program at all because there is lock shared among 10 threads.</li>
  <li>If the count of the loop is changed from 10 to infinite, the 3 threads are always activated and the rest 7 threads cannot get a chance of activating, and they are something like "deadlocked" forever.</li>
  </ul>