0

チャネル入力を優先的に処理するにはどうすればよいですか? reactWithin(0) { ... case TIMEOUT }Scala の " " コンストラクトに相当するものはありますか?

4

1 に答える 1

0

設定された間隔で優先度の高いメッセージを配信する Subscription クラスを作成しました。これは、優先順位の高いメッセージを消費するための理想的な一般的な方法ではありませんが、後世のために投稿します。他の特定のケースでは、カスタム 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 に答える