.Net max concurrent timer threads

I am trying to load a queue of interval processes. In other words, I have a queue, and I want each element in the queue to run on a separate interval.

My problem is that I cannot start more than 25 threads to start at one time. I use .Net 4.5 on a 64-bit machine, which has a maximum number of threads by default of 32768.

How to make my application run as many simultaneous threads as my machine can handle?

Here is an example application that replicates the actual problem in my production code:

class Program { static void Main(string[] args) { System.Threading.ThreadPool.SetMaxThreads(200, 200); test t = new test(); t.LoadUrls("http://www.google.com"); while (1 == 1) { System.Threading.Thread.Sleep(1000);//refresh every 5 seconds Console.WriteLine(System.Diagnostics.Process.GetCurrentProcess().Threads.Count); } } public class test { public void LoadUrls(string url) { for (int i = 0; i < 100; i++) { System.Threading.Timer t = new System.Threading.Timer(new System.Threading.TimerCallback(RunInterval), url, 0, 1000); Console.WriteLine("Loaded {0} feeds.", i); } } private static void RunInterval(object state) { string url = state as string; string data = ""; using (System.Net.WebClient cl = new System.Net.WebClient()) { Console.WriteLine("getting data for " + url); data = cl.DownloadString(url); } //do something with the data } } } 

This code should theoretically start 198 threads in 2 seconds or so.

By the way, this worked great in my application prototype; it was written in node. But now I can not get it to work correctly in C # ...

ANSWER: The problem was actually garbage collection and was not a thread problem at all; the pool is more than capable of winding all the threads that I throw at it. The trick is to use the single parameter constructor System.Threading.Timer; this will force the timer to use itself as a semaphore, while avoiding gc.

 class Program { static void Main(string[] args) { for (int i = 0; i < 100; i++) { test t = new test(); t.url = "http://www.google.com?" + i; System.Threading.Timer ti = new System.Threading.Timer(new System.Threading.TimerCallback(t.RunInterval)); ti.Change(0, 1000); } while (1 == 1) System.Threading.Thread.Sleep(int.MaxValue); } public class test { public string url { get; set; } public void RunInterval(object state) { Console.WriteLine("getting data for " + this.url); string data = ""; using (System.Net.WebClient cl = new System.Net.WebClient()) { data = cl.DownloadString(this.url); } } } } 

I'm not sure why you ever want a timer built by gc, but hey, what I know.

+7
source share
3 answers

How to make my application run as many simultaneous threads as my machine can handle?

This is the wrong approach. Each stream of roads, create a few hundred, and your system will seriously deteriorate.

Your code uses ThreadPool. The pool has an algorithm for limiting the number of threads. When you increase your sleep time (), you can see a few more threads.

A more direct way would be to install ThreadPool.MinThreads. But expect less performance, not more.

+5
source

According to System.Threading.Timer

Use a TimerCallback delegate to specify the method you want the Timer to execute. The timer delegate is specified when the timer is constructed, and cannot be changed. The method does not execute on the thread that created the timer; it executes on a ThreadPool thread supplied by the system.

Then in System.Threading.Threadpool

There is one thread pool per process. Beginning with the .NET Framework 4, the default size of the thread pool for a process depends on several factors, such as the size of the virtual address space. A process can call the GetMaxThreads method to determine the number of threads. The number of threads in the thread pool can be changed by using the SetMaxThreads method. Each thread uses the default stack size and runs at the default priority.

+2
source

you want to use the MaxDegreeOfParallelism property, but you need to change your code a bit. and then check it and determine the optimal number of threads that you want to run in parallel. here is the link: http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.maxdegreeofparallelism.aspx

+1
source

All Articles