字典c#中每个队列的线程

时间:2017-10-05 11:24:44

标签: multithreading dictionary c#-4.0 thread-safety concurrent-queue

我有一个字典,其中包含自定义类对象作为键和类队列

 ConcurrentDictionary<clsEFTOPConnection, Queue<clsEFTOPS>>();

此处clsEFTOPConnectionclsEFTOPS是自定义类。如果它不在字典中,我会添加一个新队列。现在我想在每个队列上并行工作。我希望线程监视每个队列,并且每当执行入队操作时,特定线程应该在睡眠时醒来并开始使项目出列。因此,没有任何线程等于队列中的任何一个,并且每个线程将充当其队列的观察者。  我已经写了下面的代码来实现它,但它不能按照我的需要运行

 public Queue<clsEFTOPS> CheckAlreadyExistingQueue(clsEFTOPConnection objEFTOPConnection, clsEFTOPS objJobQ)
        {
            Queue<clsEFTOPS> queue;

            // See if we have a queue
            if (!mHostQueues.TryGetValue(objEFTOPConnection, out queue))
            {
                // No queue for this Host, so create and cache
                queue = new Queue<clsEFTOPS>();
                objJobQ.Host = objEFTOPConnection.Host;
                objJobQ.PortNo = objEFTOPConnection.PortNo;
                objEFTOPConnection.socket_ = objJobQ.ReconnectSocket();
                queue.Enqueue(objJobQ);
                mHostQueues.TryAdd(objEFTOPConnection, queue);
                JobThrd = new Thread(() => DequeueJobs(queue, objEFTOPConnection));
                if (JobThrd.ThreadState != ThreadState.Running)
                    JobThrd.Start();
            }
            else
            {
                queue.Enqueue(objJobQ);
                if (queue.Count > 1)
                    JobThrd = new Thread(() => DequeueJobs(queue, objEFTOPConnection));
                else
                    DequeueJobs(queue, objEFTOPConnection);

                if (JobThrd.ThreadState != ThreadState.Running)
                    JobThrd.Start();
            }

            return queue;
        }

0 个答案:

没有答案