C#线程和队列 [英] C# Threading and Queues

查看:135
本文介绍了C#线程和队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是不是不同的方法我可以或应该使用能够利用的最佳方式的队列,而这是我所看到的发生,这是没有意义的我。

This isn't about the different methods I could or should be using to utilize the queues in the best manner, rather something I have seen happening that makes no sense to me.

void Runner() {
	// member variable
	queue = Queue.Synchronized(new Queue());
	while (true) {
		if (0 < queue.Count) {
			queue.Dequeue();
		}
	}
}

这是在单次运行主题:

var t = new Thread(Runner);
t.IsBackground = true;
t.Start();



其他的事件是排队ING别的地方。我已经看到了发生的是过了一段时间,出列实际上将抛出InvalidOperationException异常,队列为空。这应该是不可能看到作为计数如何保证有那么点意思,我敢肯定没有别的就是出列ING

Other events are "Enqueue"ing else where. What I've seen happen is over a period of time, the Dequeue will actually throw InvalidOperationException, queue empty. This should be impossible seeing as how the count guarantees there is something there, and I'm positive that nothing else is "Dequeue"ing.

问题(S):


  1. 是否有可能在排队实际上增加了计数之前的产品完全队列(这意味着什么)?

  2. 是否有可能该线程以某种方式重新启动(到期,CH375复位......)在出列语句,但之后它已经删除的项目?

编辑(澄清):

这些代码段是一个包装类的一部分实现该后台助手线程。在出列这里是唯一的出列,所有排队/出列都在同步的成员变量(队列)。

These code pieces are part of a Wrapper class that implements the background helper thread. The Dequeue here is the only Dequeue, and all Enqueue/Dequeue are on the Synchronized member variable (queue).

推荐答案

使用反射器,你可以看到没有,计数没有得到提高是增加了项目之后,直到。

Using Reflector, you can see that no, the count does not get increased until after the item is added.

由于本所指出的,它不因为你有多人打电话似乎出队。

As Ben points out, it does seem as you do have multiple people calling dequeue.

你说你是积极的,没有别的呼唤出队。是不是因为你只有一个线程调用出队?被称为出队其他地方呢?

You say you are positive that nothing else is calling dequeue. Is that because you only have the one thread calling dequeue? Is dequeue called anywhere else at all?

编辑:

我写了一个小样本代码,但不能让问题重现。它只是不停地奔跑,没有任何异常运行。

I wrote a little sample code, but could not get the problem to reproduce. It just kept running and running without any exceptions.

它是如何长时间运行你有以前的错误?也许你可以分享一些更多的代码。

How long was it running before you got errors? Maybe you can share a bit more of the code.

class Program
{
    static Queue q = Queue.Synchronized(new Queue());
    static bool running = true;

    static void Main()
    {
        Thread producer1 = new Thread(() =>
            {
                while (running)
                {
                    q.Enqueue(Guid.NewGuid());
                    Thread.Sleep(100);
                }
            });

        Thread producer2 = new Thread(() =>
        {
            while (running)
            {
                q.Enqueue(Guid.NewGuid());
                Thread.Sleep(25);
            }
        });

        Thread consumer = new Thread(() =>
            {
                while (running)
                {
                    if (q.Count > 0)
                    {
                        Guid g = (Guid)q.Dequeue();
                        Console.Write(g.ToString() + " ");
                    }
                    else
                    {
                        Console.Write(" . ");
                    }
                    Thread.Sleep(1);
                }
            });
        consumer.IsBackground = true;

        consumer.Start();
        producer1.Start();
        producer2.Start();

        Console.ReadLine();

        running = false;
    }
}

这篇关于C#线程和队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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