2

I have to implement a Delay Queue in C# as there is no standard implementation of delay queue present in the C#. I am looking to use System.Threading.Timer for implementing delayed enqueue of a node.

public class DelayQueue<T>
{
    private Queue queue<T> = new Queue<T>();

    public void Enqueue(Object object)
    {
        this.queue.Enqueue(object as T);
    }

    public void Enqueue(T node, TimeSpan dueTime)
    {
         new System.Threading.Timer(this.Enqueue, node, dueTime, -1);
    }

    .
    .
    .
}

This approach looks fine to me but since I am new to C#(from C background), I want someones opinion that whether it is the right way or are there any better and more effective methods of doing the same?

4

1 回答 1

4

我不认为为每个项目创建计时器是一个好主意。无论如何,当您从队列中取出项目时,您只需要获取第一个准备好的项目,然后您就可以存储项目准备好的时间:

public class DelayQueue<T>
{
    private List<DelayQueueItem<T>> items = new List<DelayQueueItem<T>>();

    public void Enqueue(T item)
    {
        Enqueue(item, TimeSpan.Zero);
    }

    public void Enqueue(T item, TimeSpan delay)
    {
        items.Add(new DelayQueueItem<T>()
        {
            Value = item,
            ReadyTime = DateTime.Now.Add(delay)
        });
    }

    public T Dequeue()
    {
        DateTime now = DateTime.Now;
        var item = items.FirstOrDefault(i => i.ReadyTime <= now);
        if (item != null)
        {
            items.Remove(item);
            return item.Value;
        }

        return default(T);
    }

    private class DelayQueueItem<T>
    {
        public T Value { get; set; }
        public DateTime ReadyTime { get; set; }
    }
}

更新(等待超时的阻塞队列)

public T Dequeue()
{
    return Dequeue(TimeSpan.Zero);
}

public T Dequeue(TimeSpan timeout)
{
    DateTime startTime = DateTime.Now;

    do
    {
        DateTime now = DateTime.Now;

        var item = items.FirstOrDefault(i => i.ReadyTime <= now);
        if (item == null)
            continue;

        items.Remove(item);
        return item.Value;
    }
    while (DateTime.Now - startTime < timeout);

    return default(T);
}

用法:

DelayQueue<string> queue = new DelayQueue<string>();
queue.Enqueue("world", new TimeSpan(0, 0, 1));
queue.Enqueue("hello");                        
queue.Enqueue(",");

TimeSpan timeout = new TimeSpan(0, 0, 2);
Console.WriteLine(queue.Dequeue());
Console.WriteLine(queue.Dequeue(timeout));
Console.WriteLine(queue.Dequeue(timeout));
于 2013-01-26T14:54:26.357 回答