Check C # threads

1

I'm trying to understand the code below, but to no avail. From what I've researched, GetMaxThreads returns the maximum number of available threads and GetAvailableThreads what it has available. In the case of the output below, is there no more threads available? What does .Net C # do in this situation?

int threads;
int disponiveis;
int dummy;
long consultas = Interlocked.Read(ref _threads);

ThreadPool.GetMaxThreads(out threads, out dummy);
ThreadPool.GetAvailableThreads(out disponiveis, out dummy);

_gerenciador.Log(String.Format("Status dos threads: Consultas {0}, Threads: {1}, Disponíveis: {2}", consultas, threads, disponiveis));

Output:

Status dos threads: Consultas 2, Threads: 1023, Disponíveis: 1020
    
asked by anonymous 25.09.2014 / 22:11

1 answer

2

The threads indication does not mean that these threads were actually created. The ThreadPool works with a maximum number of threads (possible to be configured), the GetMaxThreads method returns that number, whereas GetAvailableThreads returns the number of threads in> available (Max - In Use).

If threads are exhausted, ThreadPool will wait for the end of some thread in use to reuse it for the new job.

Note that even though ThreadPool is out of order, you can still create new threads at hand: new Thread(Metodo).Start() / p>

Only code snippets that use ThreadPool will not have their jobs executed immediately.

    
29.09.2014 / 18:35