0

How to make a ConcurrentQueue to be cleaned by the condition for the first elements? Eg to clear older blog posts. I came up with this idea of a ConditionConcurrentQueue:

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;

public class ConditionConcurrentQueue<T> : ConcurrentQueue<T>
    where T: class
{
    public ConditionConcurrentQueue(Func<T, bool> condition)
        : this(null, condition)
    { }

    public ConditionConcurrentQueue(IEnumerable<T> items, Func<T, bool> condition)
        : base(items)
    {
        _condition = condition;
    }

    private Func<T, bool> _condition;

    public virtual void Enqueue(T item)
    {
        T removed;
        bool cleaningRun = true;
        int failedCnt = 0;

        while (!IsEmpty && cleaningRun && failedCnt < 10)
        {
            if (TryPeek(out removed))
            {
                bool result = _condition.Invoke(removed);

                if (!result)
                {
                    if (!TryDequeue(out removed))
                    {
                        failedCnt++;
                        Thread.Sleep(10);
                    }
                }
                else
                    cleaningRun = false;
            }
            else
            {
                failedCnt++;
                Thread.Sleep(10);
            }
        }

        base.Enqueue(item);
    }
}

Use this ConditionConcurrentQueue could be so:

class Blog
{
    public ConditionConcurrentQueue<Post> Posts { get; set; }
}

class Post
{
    public DateTime Time { get; set; }

    public string Text { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Blog blog = new Blog
        {
            Posts = new ConditionConcurrentQueue<Post>(
            new Post[] { 
                         new Post { Time = DateTime.Now - TimeSpan.FromMinutes(80), Text = "Title 1" },
                         new Post { Time = DateTime.Now - TimeSpan.FromMinutes(60), Text = "Title 2" },
                         new Post { Time = DateTime.Now - TimeSpan.FromMinutes(40), Text = "Title 3" },
                       },
            p => p.Time > DateTime.Now - TimeSpan.FromHours(1))
        };

        blog.Posts.Enqueue(new Post { Time = DateTime.Now - TimeSpan.FromMinutes(20), Text = "Title 4" });

        foreach (Post post in blog.Posts.ToList())
            Console.WriteLine(post.Text);
    }
}

Maybe it is too primitive solution. I would appreciate any improvements. Thanks.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
FreeClimb
  • 696
  • 8
  • 13

2 Answers2

1

also, you can try through the extension method:

    public static ICollection<T> Enqueue<T>(this ConcurrentQueue<T> field, T item, Func<T, bool> predicate)
    {
        ICollection<T> removed = field.TryDequeue<T>(predicate);

        field.Enqueue(item);

        return removed;
    }

    public static ICollection<T> TryDequeue<T>(this ConcurrentQueue<T> field, Func<T, bool> predicate)
    {
        T comparedItem;
        var removedList = new List<T>();

        while (field.TryPeek(out comparedItem))
        {
            if (!predicate.Invoke(comparedItem))
            {
                if (field.TryDequeue(out comparedItem))
                    removedList.Add(comparedItem);
                else
                    break;
            }
            else
                break;
        }

        return removedList;
    }
FreeClimb
  • 696
  • 8
  • 13
0

As of .NET Core 2.0 / .NET Standard 2.1 / .NET Framework 5.0, there is a Clear() method on ConcurrentQueue<T>. See: ConcurrentQueue.Clear.

olabacker
  • 1,232
  • 1
  • 16
  • 27