如何以优先方式处理通道输入?有什么等同于 Scala 的 " reactWithin(0) { ... case TIMEOUT }
" 构造的吗?
问问题
765 次
1 回答
0
我编写了一个订阅类,它在设定的时间间隔内传递优先消息。这不是使用优先消息的理想一般情况方式,但我会为后代发布它。我认为对于某些其他情况,自定义 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);
}
}
于 2009-08-10T07:06:14.300 回答