Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6550755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:15:16+00:00 2026-05-25T12:15:16+00:00

The code: static void DoIt(string name) { Console.WriteLine(Hello {0} | {1}, name, Thread.CurrentThread.ManagedThreadID); Thread.Sleep(5000);

  • 0

The code:

static void DoIt(string name)
{
    Console.WriteLine("Hello {0} | {1}", name, Thread.CurrentThread.ManagedThreadID);
    Thread.Sleep(5000);
    Console.WriteLine("Bye {0} | {1}", name, Thread.CurrentThread.ManagedThreadID);
}

static void Main()
{
    Task.Factory.StartNew(() => DoIt("One"));
    Task.Factory.StartNew(() => DoIt("Two"));
    Task.Factory.StartNew(() => DoIt("Three"));
    Task.Factory.StartNew(() => DoIt("Four"));
    Task.Factory.StartNew(() => DoIt("Five"));
    Task.Factory.StartNew(() => DoIt("Six"));
    Task.Factory.StartNew(() => DoIt("Seven"));
    Task.Factory.StartNew(() => DoIt("Eight"));
    Task.Factory.StartNew(() => DoIt("Nine"));
    Task.Factory.StartNew(() => DoIt("Ten"));

    Console.ReadKey();
}

How come that it can fine start the first 3 Tasks immediately, but then it takes 5-10sec for Task 4 to start, and after Task 4 have started, then it takes 5-10sec before Task 5 starts and so on. Is it the GC thats doing something? Could someone please clarify whats happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:15:17+00:00Added an answer on May 25, 2026 at 12:15 pm

    How come that it can fine start the first 3 Tasks immediately, but then it takes 5-10sec for Task 4 to start, and after Task 4 have started, then it takes 5-10sec before Task 5 starts and so on. Is it the GC thats doing something? Could someone please clarify whats happening?

    By default, the first time you run this, the ThreadPool is allocated using the minimum number of worker threads. After the first 4 tasks are scheduled, the threadpool will “ramp up” to handle more over time, which is why you see the delay.

    On my system (which has 8 cores), the first 8 are instantanteous, then the next two start up one second later.

    In your case, if you run your test two times, the second time, the threads will all start up immediately. This is because, after the first run, the ThreadPool should have enough workers to schedule this right away.

    Try the following to see this behavior in action. If you leave the SetMinThreads call in place, these will all schedule immediately. If you comment it out, you’ll see that, the first time, it takes a while, but the second time through (provided you wait for the threads to complete), the threads will run immediately.

    static void DoIt(string name)
    {
        Console.WriteLine("Hello {0} | {1} - {2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now);
        Thread.Sleep(5000);
        Console.WriteLine("Bye {0} | {1} - {2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now);
    }
    
    static void Main()
    {
        int workerThreads, complete;
        ThreadPool.GetMinThreads(out workerThreads, out complete);
    
        Console.WriteLine(workerThreads);
    
        // Comment out this line to see the difference...
        // WIth this commented out, the second iteration will be immediate
        ThreadPool.SetMinThreads(100, complete);
    
        Action run = () =>
            {
                for (int i = 0; i < 20; ++i)
                {
                    int tmp = i;
                    Task.Factory.StartNew(() => DoIt(tmp.ToString()));
                }
            };
    
        run();
        Console.WriteLine("Press a key to run again...");
        Console.ReadKey();
    
        run();
    
        Console.WriteLine("Press a key to exit...");
        Console.ReadKey();
    }
    

    Note that this behavior actually has little to do with the TPL as a whole – it’s more the default TaskScheduler used which just passes off the tasks to the ThreadPool. If you were to set these threads up with the LongRunning hint in your StartNew() call, for example, they’d all start immediately (since the default scheduler will setup a new, dedicated thread and execute it immediately).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: private static void WriteProcesses(StreamWriter sw, DateTime d) { sw.WriteLine(List of
Here is my code : public static void main(String[] args) { // System.setProperty( //
i have the following code: public static void Serialize() { List<string> dirs = FileHelper.GetFilesRecursive(fileDirectoryPath);
I have the following code in Java: public static<T> void doIt(Class<T> t) { T[]
Code static void MyClass::ThreadEntryStatic() { //... } void MyClass::Begin() { CreateThread(..,ThreadEntryStatic,..); } In which
I have following code: public static void ProcessStep(Action action) { //do something here if
I have the following code: private static void WriteStartupInfo() { Settings settings = Settings.Default;
When i have a code block static void Main() { foreach (int i in
Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
Here's the sample code: public static void col (int n) { if (n %

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.