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! :)
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.....
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With