【发布时间】:2010-08-17 13:30:04
【问题描述】:
嘿,我正在尝试在 C# 中为异步服务器实现 ConcurrentQueue。收到完整消息后,项目将立即排队。为了使消息出列,我正在制作少量线程来完成出列和服务请求的工作。这是不够的,因为每个线程都使用一个 while 循环,这会消耗相当多的处理器时间,原因很明显。
有谁知道在需要时使消息出列但不消耗太多处理时间的方法。
{
...
for (int i = 0; i < 3; i++)
{
Thread t = new Thread(new ThreadStart(startParsingMessages));
t.Start();
}
...
}
private void startParsingMessages()
{
QueueContainer dequeued = null;
Console.WriteLine("Trying");
while (true)
{
if (queue.TryDequeue(out dequeued))
{
Console.WriteLine("processing queue");
ProcessMessage(dequeued.socket, dequeued.message);
}
}
}
【问题讨论】: