Can objects be added in bulk to a Queue.

  • Thread starter William Stacey [MVP]
  • Start date
W

William Stacey [MVP]

Hi Peter, I don't see one either. I would guess they did not include such a
"convenience" method because it may not make sense in some implementations
with many producers and/or consumers. For example, you may want to lock the
queue and put all elements in your collection one after the other in order.
In other cases, you may want to allow other producers or consumers to get
and put objects fairly depending on what thread has the cpu and put your
elements into the queue as you get access to allow fairness and prevent
blocking other producers, etc. So they would have to decide for you what
would be best and could be wrong in some cases. As you know, you can do
this pretty easy with your own helper method and iterate over your input
collection and do an Enqueue for each object. You could also write a thin
wrapper around the Queue and add this method yourself. At first blush, I
would not think that iterating over your collection and Putting each element
would be much slower then using something like Array.Copy (like ArrayList
uses) to append them to the internal Array in the general case (could be
wrong and have not tested this for speed.) If you do a lot of this type of
appending collections, you may want to create your own Queue implementation
to get direct access to your internal array structure so you could use
things like Array.Copy, unsafe code, etc. hth
 
P

Peter Rilling

After looking at the documentation for the Queue class I became
disappointed, or maybe I just did not see what I want. Is there any way to
add a batch of objects to the queue similar to the AddRange method on other
collections, or do I have to manually add them one at a time? I don't mean
in the constructor. I want to add objects after the Queue has been
constructed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top