如何以优先级调度我的线程 [英] How do I dispatch my threads with a priority

查看:99
本文介绍了如何以优先级调度我的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个需要解析的消息队列。有些消息很紧急,需要先处理。



我还需要使用线程。以线性方式处理这些消息的速度太慢了。



在高峰时段,我可以积压数千个。



我尝试使用ThreadPool。速度显着提高,但我不能(或不知道如何)首先处理优先级消息。



我尝试使用Dispatcher,但没有一个线程实际运行:

  public   delegate   void  NextPrimeDelegate(); 
private Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;

public void ParseMessageQueue()
{

// Projections消息是对实例的db引用。
WorkflowDataAccess。 Projections.Message消息;

// 从队列中获取下一条消息。如果队列为空,那么它将等待该线程直到一个到达。所有这些消息都有'read'= true
((message = _messageQueue.Next)!= null
{
// 调试消息
Logs.Log( string .Format( @ 3.消息ID:{0},message.Id));

// 获取m的循环安全版
< span class =code-keyword> var m = message;
// 将方法和项目排队

var next = new NextPrimeDelegate(()= > {CallBack(m);});
DispatcherPriority dispatcherPriority;
switch (m.Urgency)
{
case 0
dispatcherPriority = DispatcherPriority.Normal;
break ;
默认
dispatcherPriority = DispatcherPriority.Send;
break ;
}

// 回调无法运行> _<
_dispatcher.BeginInvoke(dispatcherPriority,next);

// 没有优先级的线程
// ThreadPool.QueueUserWorkItem(CallBack,m,);

// 线性
// 回拨(m);
}
}





也许我是做错了吗?



如何建议如何提高性能并优先处理紧急信息?



谢谢^ _ ^

Andy

解决方案

参考点 [ ^ ]不要触摸线程池的优先级



你可能会发现这个链接 [ ^ ]有趣

Hi,

I have a message queue that I need to parse. Some messages are urgent and need to be processed first.

I also need to use threading. Processing these messages in a linear fashion is far too slow.

At peak times I could have a backlog of thousands.

I tried using a ThreadPool. The speed was significantly improved but I could not (or don't know how to) process priority messages first.

I tried using a Dispatcher, but none of the threads actually run:

public delegate void NextPrimeDelegate();
private Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;

public void ParseMessageQueue()
{

    //Projections message is a db reference to the instance.
    WorkflowDataAccess.Projections.Message message;

    //Get next message from the queue.  If the queue is empty then it will wait this thread until one arrives.  All these messages have 'read'=true
    while ((message = _messageQueue.Next) != null)
    {
    //Debug message
    Logs.Log(string.Format(@"3. Message Id: {0}", message.Id));

        //Get a loop safe version of m
        var m = message;
        //Queue the method and item

        var next = new NextPrimeDelegate(() => { CallBack(m); });
        DispatcherPriority dispatcherPriority;
        switch (m.Urgency)
        {
            case 0:
                dispatcherPriority=DispatcherPriority.Normal;
                break;
            default:
                dispatcherPriority = DispatcherPriority.Send;
                break;
        }

        //Callback does not get run >_<
        _dispatcher.BeginInvoke(dispatcherPriority, next);
    
        //threads without priority
        //ThreadPool.QueueUserWorkItem(CallBack, m,);

        //Linear
        //CallBack(m);
    }
}



Maybe I'm doing it wrong?

Any suggestions how I can improve the performance and prioritize the urgent messages?

Thanks ^_^
Andy

解决方案

A good reference point[^] on not touching priority of Thread Pool

You might find this link[^] interesting


这篇关于如何以优先级调度我的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆