Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Thread Number in Thread Pool

Tags:

c#

threadpool

When I write the below code, why do I get avaliable Thread number like 1022, 1020. I have to get 25 thread max as I am using thread pool.

I guess the ouput thread number is the avaliable threads on the system. I need to get the avaliable thread number in my thread pool, in win form application.

private void Foo()
{
    int intAvailableThreads, intAvailableIoAsynThreds;

    // ask the number of avaialbe threads on the pool,
    //we really only care about the first parameter.
    ThreadPool.GetAvailableThreads(out intAvailableThreads,
        out intAvailableIoAsynThreds);

    // build a message to log
    string strMessage =
        String.Format(@"Is Thread Pool: {1},
            Thread Id: {2} Free Threads {3}",
            Thread.CurrentThread.IsThreadPoolThread.ToString(),
            Thread.CurrentThread.GetHashCode(),
            intAvailableThreads);

    // check if the thread is on the thread pool.
    Trace.WriteLine(strMessage);

    // create a delay...
    Thread.Sleep(30000);

    return;
}

Thanks alot..

(Note : I got the code from http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx) Nice article!!

like image 416
Yagiz Ozturk Avatar asked Mar 08 '11 18:03

Yagiz Ozturk


People also ask

How do I find the number of active threads?

The java. lang. Thread. activeCount() method returns the number of active threads in the current thread's thread group.

How do you get active threads from thread pool executor?

Use a ThreadPoolExecutor implementation and call getActiveCount() on it: int getActiveCount() // Returns the approximate number of threads that are actively executing tasks.

What are active threads?

An active java thread is a thread that is eligible to be the currently running thread of execution. It is a thread that has left the "new" state and has attained (or re-attained) the "runnable" state. Active threads take up residency in the runnable thread pool.

What is thread and thread pool?

In computer programming, a thread pool is a software design pattern for achieving concurrency of execution in a computer program. Often also called a replicated workers or worker-crew model, a thread pool maintains multiple threads waiting for tasks to be allocated for concurrent execution by the supervising program.


3 Answers

I have to get 25 thread max as I am using thread pool.

The maximum number of threads in the thread-pool per core has changed a lot over time. It's no longer 25 per core, which I suspect is what you were expecting.

For example, running this on my quad-core hyperthreaded laptop with .NET 4, I get a maximum of 32767 worker threads and 1000 IO completion port threads:

using System;
using System.Threading;

class Test
{
    static void Main()
    {
        int worker; 
        int ioCompletion;
        ThreadPool.GetMaxThreads(out worker, out ioCompletion);
        Console.WriteLine("{0} / {1}", worker, ioCompletion);
    }    
}

Under .NET 3.5 I get 2000 worker threads and still 1000 IO completion port threads.

That isn't the number of threads actually in the thread pool though - it's the maximum number that the thread pool allows itself to create over time.

like image 64
Jon Skeet Avatar answered Oct 27 '22 01:10

Jon Skeet


Use ThreadPool.GetMaxThreads to get what you want.

This article helps explain everything you need to know about ThreadPool.

like image 40
John Arlen Avatar answered Oct 26 '22 23:10

John Arlen


I set both to 10001, using ThreadPool.SetMaxThread. After that whenever, I run your program it shows max thread 10001, its persisted. I didn't restarted my core i 5 system.

After that I set both it to 10000000 , and it is showing following thread max count

worker:32767 ioCompletion:32767

So on my Windows 7 Intel Core i5 , this is the maximum capacity.

like image 40
Imran Rizvi Avatar answered Oct 27 '22 00:10

Imran Rizvi