none
Concurrentbag vs Blockingcollection RRS feed

  • Question

  • I have a telephony application, in which there are many calls. Each call can be done with a process(method). Now I want to make 100 call simultaneously. I also want to return the call result(success or fail). Of course thread safe is required. Which one should I use?

    Concurrentbag vs Blockingcollection or something else?

    And I can limit the upbound "100" in the collection?

    Monday, July 28, 2014 1:37 PM

Answers

  • You can delay a task by using the Delay method off of the task.  But I'm curious how that will interplay with the constrained scheduler.  You wouldn't want the task to be delayed for 3 hours but still taking up one of your 100 runnable tasks.  You'd have to play around with it to see how it behaves.  It might be necessary to track the "delayed" work in a separate collection that then generates a task when the time has elapsed.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by ardmore Monday, July 28, 2014 7:22 PM
    Monday, July 28, 2014 7:17 PM
    Moderator

All replies

  • Depends upon your requirements.  A Concurrentbag is nothing more than a collection of values.  It is thread safe to work with so if you just want a grab bag of objects then it'll work. 

    BlockingCollection is similar but it allows you to limit how many objects are in the collection.  This has an impact on your callers because if they try to add an object and the collection is full then they block.  If you want that functionality then this is the type for you. 

    Given your limited description I wouldn't say that either type is necessarily the correct one.  Given a choice I'd probably lean toward the bag because blocking an add might not make sense.  If all you're doing is storing data about work you need to do then a blocking collection is probably not what you want to do.  If your collection is more of a work queue then a simple concurrent collection is sufficient so you can pull work off on a secondary thread.  How much work there is probably doesn't matter.  But if you need to cap how long you can wait for the work to complete then a blocking collection may be the correct solution.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Monday, July 28, 2014 2:42 PM
    Moderator
  • @Michael.

    Sorry for the limit information provided. Because the telephony has limited ports, say 100 ports(lines), therefore it can take the maximum 100 calls simultaneously. If there is 101th call comes, it has to wait until one call pops out.

    So what choice?

    SemaphoreSlim, ParallelFor, Concurrentbag, BlockingCollection or just Concurrent?

    Monday, July 28, 2014 3:03 PM
  • It depends upon how you're processing the calls.  If each call gets its own worker thread then you can use a blocking collection to store the threads that will do the work.  This is a lot like the standard thread pool thread so you might just be able to get away with using that.  If you go that route then when you try to retrieve the 101st thread to handle the connection you'll block until one is available.

    Perhaps a better approach though is to use the TPL. This eliminates the need for a blocking collection and thread management.  It also uses the TPL which is the preferred async approach these days.  For this to work you can create a new task factory that limits the # of tasks that can be running at any one time.  MSDN has an example here. Using this factory you will create a new task to run. When it reaches its limit it'll block until there is room. This gives you the blocking and async work altogether.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Monday, July 28, 2014 6:31 PM
    Moderator
  • One more request.

    Some of calls are intended to be delay. For example, to make a call from NY to CA, since there are 3 hours time difference. We always want the call not happening in the deep night because of caller party sleep. Can we still select TPL?

    Monday, July 28, 2014 7:09 PM
  • You can delay a task by using the Delay method off of the task.  But I'm curious how that will interplay with the constrained scheduler.  You wouldn't want the task to be delayed for 3 hours but still taking up one of your 100 runnable tasks.  You'd have to play around with it to see how it behaves.  It might be necessary to track the "delayed" work in a separate collection that then generates a task when the time has elapsed.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by ardmore Monday, July 28, 2014 7:22 PM
    Monday, July 28, 2014 7:17 PM
    Moderator