none
Monitoring the threads spawned by Task Factory RRS feed

  • Question

  • I have created a method which is called by Task.Factory.Startnew

    The actual method pulls the messages from an MSMQ and spawns n no. of threads based on the no of messages received

    from MSMQ. I need to monitor the no of threads spawned by the task factory and identify the time taken to complete

    each task. And I should also find how many new threads are created and find when does all the threads complete

    process/action. can any of you suggest how i can achieve this?

    Ex:- (like the following)

    static void Main()    // The Task class is in System.Threading.Tasks
    {
      Task.Factory.StartNew (Go);
    }
     
    static void Go()
    {
      Console.WriteLine ("Hello from the thread pool!");
    }



    Software Engineer

    Friday, April 20, 2012 2:31 AM

Answers

  • when does all the threads complete

    // Wait for all tasks to complete.
    Task[] tasks = new Task[10];
    for (int i = 0; i < 10; i++)
    {
     tasks[i] = Task.Factory.StartNew(() 
                         => DoSomeWork(10000000));
    }
    Task.WaitAll(tasks);

    How many new threads are created and find when does all the threads complete ?

    void DoSomeWork(int val)
     {
     Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();
    int threadId = Thread.CurrentThread.ManagedThreadId; 
      // Pretend to do something.
    Thread.Sleep(1000);
    //
    stopwatch.Stop();
    Console.WriteLine("Time elapsed: {0}",
    	    stopwatch.Elapsed);
      }

    Every task receives an integer ID that uniquely identifies it in an application domain and that is accessible by using the Id property if needed you can use that as well.

    For Completion notification, I am aware of Task.ContinueWith.  This sets up a task that runs after the task completes.  This task could raise an event as supplied method.

    No of thread is not span by TaskFactory actually, It is done by Task Scheduler and Thread Pool with a sweet concept called concurrency control algorithm (Do not confuse from Work-Stealing Algo..). The purpose of the concurrency control algorithm in CLR 4.0 is to automatically decide how many work items can be run concurrently in an efficient manner, hence optimizing the throughput of the Thread Pool. Before creating a new thread CLR does apply a heuristic analysis with a lot of noise and parameters such as the type of workload and resource contention etc. It also depends on the assumption that every work item is a useful piece of work.

    Refer: http://msdn.microsoft.com/en-us/magazine/ff960958.aspx

    NB: We have a specialized forum for Parallel Extensions to the .NET Framework where some expert can help you if you move or post your question specific to TPL.


    Lingaraj Mishra

    Friday, April 20, 2012 12:09 PM

All replies

  • Hi,

    Since you are using Task, which is well designed for concurrent and asynchronous coding, we need to worry about the internal design and how Task uses ThreadPool to do the asynchronous jobs.  Could you please let me know why do you want to monitor the Thread behaviors? 

    Here is a topic about monitoring the ThreadPool, http://msdn.microsoft.com/en-us/library/ms973903.aspx#threadpool_topic7.  

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us

    Friday, April 20, 2012 9:08 AM
    Moderator
  • when does all the threads complete

    // Wait for all tasks to complete.
    Task[] tasks = new Task[10];
    for (int i = 0; i < 10; i++)
    {
     tasks[i] = Task.Factory.StartNew(() 
                         => DoSomeWork(10000000));
    }
    Task.WaitAll(tasks);

    How many new threads are created and find when does all the threads complete ?

    void DoSomeWork(int val)
     {
     Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();
    int threadId = Thread.CurrentThread.ManagedThreadId; 
      // Pretend to do something.
    Thread.Sleep(1000);
    //
    stopwatch.Stop();
    Console.WriteLine("Time elapsed: {0}",
    	    stopwatch.Elapsed);
      }

    Every task receives an integer ID that uniquely identifies it in an application domain and that is accessible by using the Id property if needed you can use that as well.

    For Completion notification, I am aware of Task.ContinueWith.  This sets up a task that runs after the task completes.  This task could raise an event as supplied method.

    No of thread is not span by TaskFactory actually, It is done by Task Scheduler and Thread Pool with a sweet concept called concurrency control algorithm (Do not confuse from Work-Stealing Algo..). The purpose of the concurrency control algorithm in CLR 4.0 is to automatically decide how many work items can be run concurrently in an efficient manner, hence optimizing the throughput of the Thread Pool. Before creating a new thread CLR does apply a heuristic analysis with a lot of noise and parameters such as the type of workload and resource contention etc. It also depends on the assumption that every work item is a useful piece of work.

    Refer: http://msdn.microsoft.com/en-us/magazine/ff960958.aspx

    NB: We have a specialized forum for Parallel Extensions to the .NET Framework where some expert can help you if you move or post your question specific to TPL.


    Lingaraj Mishra

    Friday, April 20, 2012 12:09 PM
  • Hi,

    Any update of this issue? 

    Have a nice weekend!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us

    Friday, April 27, 2012 1:49 AM
    Moderator