Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit Threads count

I have a List with items that I want to download. I use a for Loop to iterate the list.

For each item in this List I start a new Thread that references the item. My Problem is that I want limit the maxDownload at the same time.

for (int i = downloadList.Count - 1; i >= 0; i--)
{
    downloadItem item = downloadList[i];
    if (item.Status != 1 && item.Status != 2)
    {
        ThreadStart starter = delegate { this.DownloadItem(ref item); };
        Thread t = new Thread(starter);
        t.IsBackground = true;
        t.Name = item.Name;
        t.Priority = ThreadPriority.Normal;
        t.Start();
    }
}

I read something about the ThreadPool, but then I can't reference my item. Can someone Help me? Thanks! :)

Edit:

I tested this:

ThreadPool.SetMaxThreads(maxDownloads, maxDownloads);
ThreadPool.SetMinThreads(maxDownloads, maxDownloads);
ThreadPool.QueueUserWorkItem(DownloadItem, ref item);

I don't know how I can reference my downloadItem with this thread.....

like image 970
Werewolve Avatar asked Jul 26 '10 10:07

Werewolve


People also ask

Is there a limit to number of threads?

The kernel parameter threads-max controls the maximum number of threads. This parameter is defined in the file /proc/sys/kernel/threads-max. Here, the output 63704 indicates that the kernel can execute a maximum of 63,704 threads.

How do I limit the number of threads in Java?

Each JVM server can have a maximum of 256 threads to run Java applications. In a CICS region you can have a maximum of 1024 threads. If you have many JVM servers running in the CICS region, you cannot set the maximum value for every JVM server.

How many threads can Windows 10 handle?

32-bit processes can only address 4 GB of memory, which will fit about 2,000 threads with the default 1 MB stack allocation per thread or about 12,000 with the smallest possible allocation of 64 KB per thread. 64-bit processes don't have problems with address space, but rather with the actual allocations.

What happens if you have too many threads?

Problem with spawning multiple threads is, you will loose the performace because when number of threads are greater than number or cores then there will be context switching for processor time. One need to understand in any given point of time on a single core, only one thread can run.


1 Answers

if you're using .NET 4, I'd strongly suggest using Parallel.ForEach (potentially on downloadList.Reverse())

so, something like:

Parallel.ForEach(downloadList.Reverse(), 
                 new ParallelOptions { MaxDegreeOfParallelism = 8 },
                 item => this.DownloadItem(item));

If you don't want the calling thread to block, you could QueueUserWorkItem this call, of course.

like image 68
James Manning Avatar answered Oct 03 '22 15:10

James Manning