代码之家  ›  专栏  ›  技术社区  ›  anthony

Retlang中的通道输入优先级

  •  0
  • anthony  · 技术社区  · 16 年前

    如何以优先方式处理渠道输入?有同等的东西吗 斯卡拉的“ reactWithin(0) { ... case TIMEOUT } “构造?

    1 回复  |  直到 9 年前
        1
  •  0
  •   anthony    15 年前

    我编写了一个订阅类,该类按设置的时间间隔传递优先消息。这不是使用优先消息的理想的一般情况,但我会将其发布给子孙后代。我认为对于某些其他情况,自定义的requestreplychannel是更好的选择。PriorityQueue的实现留给读者作为练习。

    class PrioritySubscriber<T> : BaseSubscription<T>
    {
        private readonly PriorityQueue<T> queue;
        private readonly IScheduler scheduler;
        private readonly Action<T> receive;
        private readonly int interval;
    
        private readonly object sync = new object();
        private ITimerControl next = null;
    
        public PrioritySubscriber(IComparer<T> comparer, IScheduler scheduler,
            Action<T> receive, int interval)
        {
            this.queue = new PriorityQueue<T>(comparer);
            this.scheduler = scheduler;
            this.receive = receive;
            this.interval = interval;
        }
    
        protected override void OnMessageOnProducerThread(T msg)
        {
            lock (this.sync)
            {
                this.queue.Enqueue(msg);
    
                if (this.next == null)
                {
                    this.next =
                        this.scheduler.Schedule(this.Receive, this.interval);
                }
            }
        }
    
        private void Receive()
        {
            T msg;
    
            lock (this.sync)
            {
                msg = this.queue.Dequeue();
    
                if (this.queue.Count > 0)
                {
                    this.next =
                        this.scheduler.Schedule(this.Receive, this.interval);
                }
            }
    
            this.receive(msg);
        }
    }