문제

채널 입력은 어떻게 우선 순위로 처리 될 수 있습니까? Scala 's와 동등한 것이 있습니까? "reactWithin(0) { ... case TIMEOUT }" 건설하다?

도움이 되었습니까?

해결책

정해진 간격으로 우선 순위를 정한 메시지를 전달하는 구독 클래스를 작성했습니다. 우선 순위가 큰 메시지를 소비하는 이상적인 일반적인 경우는 아니지만 후손을 위해 게시하겠습니다. 사용자 정의 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);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top