1

如何使 ConcurrentQueue 被第一个元素的条件清除。例如清除较旧的博客文章。我想出了一个 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);
    }
}

使用这个 ConditionConcurrentQueue 可能是这样的:

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);
    }
}

也许这是太原始的解决方案。我将不胜感激任何改进。谢谢。

4

2 回答 2

1

另外,您可以尝试通过扩展方法:

    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;
    }
于 2016-04-05T18:47:16.290 回答
0

从 .NET Core 2.0 / .NET Standard 2.1 / .NET Framework 5.0 开始,在 .NET 上有一个Clear()方法ConcurrentQueue<T>。请参阅:ConcurrentQueue.Clear

于 2020-10-29T12:24:54.627 回答