Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:15:14+00:00 2026-06-10T03:15:14+00:00

I created a pool class that when asked, finds or initializes an item and

  • 0

I created a pool class that when asked, finds or initializes an item and return it to the caller. I use a ConcurrentQueue<T> as the underlying collection but I’m not sure whether it’s the right type to use.

I don’t need the items to be in any specific order, all I need is a thread-safe collection I can push to and pop from. Does .NET have a fast collection class for this purpose?

Edit: I used ConcurrentBag<T> after spender’s answer:

public sealed class Pool<T>
{
    private readonly Func<T> initializer;

    private readonly ConcurrentBag<T> bag;

    public Pool(Func<T> initializer)
    {
        if (initializer == null)
            throw new ArgumentNullException("initializer");

        this.initializer = initializer;
        this.bag = new ConcurrentBag<T>();
    }

    public Pool(Func<T> initializer, IEnumerable<T> collection)
    {
        if (initializer == null)
            throw new ArgumentNullException("initializer");

        if (collection == null)
            throw new ArgumentNullException("initializer");

        this.initializer = initializer;
        this.bag = new ConcurrentBag<T>(collection);
    }

    public Pool(Func<T> initializer, int allocationCount)
        : this(initializer)
    {
        if (allocationCount < 0)
            throw new ArgumentOutOfRangeException("allocationCount");

        for (int i = 0; i < allocationCount; i++)
            this.bag.Add(initializer.Invoke());
    }

    public void Push(T item)
    {
        this.bag.Add(item);
    }

    public T Pop()
    {
        T item;
        return this.bag.TryTake(out item)
            ? item
            : this.initializer.Invoke();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:15:16+00:00Added an answer on June 10, 2026 at 3:15 am

    ConcurrentBag sounds like just the ticket. You’ll need to deal with the empty case yourself, but it seems to fit your needs quite nicely. In particular, ConcurrentBag.TryTake will grab an item from the collection with no guarantee that any particular one will be returned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created this generic pool to re-use some sprites that i add to the
I have created this Pool that has 5 static variables. public class FruitPool extends
I have a custom thread pool class, that creates some threads that each wait
I want to have singleton class that its object is not statically created. having
I've created a custom thread pool utility, but there seems to be a problem
I created a class that uses the the Java Executor API to create/manage a
I have a class that I've created to allow asynchronous sequential execution of tasks,
I would like to create a thread pool. I have a class called ServerThread.cpp,
We're running our default website in a .Net 2.0 app pool. We've since created
This question has been asked a lot of times in many forums. But I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.