So, I’ll make an application for checking links if they’re accessible(live).
My question is how to make the threads “always busy”. What I mean:
The app run 100 threads(created with FOR loop for example) with 100 different URLs. So when 1 of the threads finish it’s job(check if URL is available) to get new URL and start again immediately. So the 100 threads will work non-stop till all URLs are checked.
How can I accomplish that?
What you are looking for is called the Producer-Consumer Model. You have a pool of resources, that contains the list of urls to check, one thread can fill that pool, and your conumer threads can pull from that pool, if you have .NET 4 Parallel.ForEach does most of the work for you.
Using 100 threads also is very likely not going to be the optimum number of threads, just let the Task Parallel Library manage the thread count for you.
Here is a example if the list will be pre-populated and not have more items added as the thread is running.
If you need to populate the collection as it runs, replace
List<string>with a concurrent collection like BlockingCollectionI also wanted to add that the automated thread scheduling may not be the best also, it may put some limits that could be expanded on, see this comment from the original question