C# thread pool limiting threads

后端 未结 2 1784
不思量自难忘°
不思量自难忘° 2020-12-02 11:35

Alright...I\'ve given the site a fair search and have read over many posts about this topic. I found this question: Code for a simple thread pool in C# especially helpful.

相关标签:
2条回答
  • 2020-12-02 11:45

    Note: if you are limiting this to "3" just so you don't overwhelm the machine running your app, I'd make sure this is a problem first. The threadpool is supposed to manage this for you. On the other hand, if you don't want to overwhelm some other resource, then read on!


    You can't manage the size of the threadpool (or really much of anything about it).

    In this case, I'd use a semaphore to manage access to your resource. In your case, your resource is running the web scrape, or calculating some report, etc.

    To do this, in your static class, create a semaphore object:

    System.Threading.Semaphore S = new System.Threading.Semaphore(3, 3);
    

    Then, in each thread, you do this:

    System.Threading.Semaphore S = new System.Threading.Semaphore(3, 3);
    
    try
    {
        // wait your turn (decrement)
        S.WaitOne();
        // do your thing
    }
    
    finally {
        // release so others can go (increment)
        S.Release();
    }
    

    Each thread will block on the S.WaitOne() until it is given the signal to proceed. Once S has been decremented 3 times, all threads will block until one of them increments the counter.

    This solution isn't perfect.


    If you want something a little cleaner, and more efficient, I'd recommend going with a BlockingQueue approach wherein you enqueue the work you want performed into a global Blocking Queue object.

    Meanwhile, you have three threads (which you created--not in the threadpool), popping work out of the queue to perform. This isn't that tricky to setup and is very fast and simple.

    Examples:

    • Best threading queue example / best practice
    • Best method to get objects from a BlockingQueue in a concurrent program?
    0 讨论(0)
  • 2020-12-02 11:48

    It's a static class like any other, which means that anything you do with it affects every other thread in the current process. It doesn't affect other processes.

    I consider this one of the larger design flaws in .NET, however. Who came up with the brilliant idea of making the thread pool static? As your example shows, we often want a thread pool dedicated to our task, without having it interfere with unrelated tasks elsewhere in the system.

    0 讨论(0)
提交回复
热议问题