Skip to content

Instantly share code, notes, and snippets.

@heiswayi
Created January 31, 2021 05:48
Show Gist options
  • Save heiswayi/13d6dead9fba69c18d6bbe4afef96e9d to your computer and use it in GitHub Desktop.
Save heiswayi/13d6dead9fba69c18d6bbe4afef96e9d to your computer and use it in GitHub Desktop.
public class FixedSizedQueue<T> : ConcurrentQueue<T>
{
private readonly object syncObject = new object();
public int Size { get; private set; }
public FixedSizedQueue(int size)
{
Size = size;
}
public new void Enqueue(T obj)
{
base.Enqueue(obj);
lock (syncObject)
{
while (base.Count > Size)
{
T outObj;
base.TryDequeue(out outObj);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment