我在C#(4.0)中有一個常規的隊列對象,並且我使用了訪問此隊列的BackgroundWorkers。排隊併發隊列
我使用的代碼如下:
do
{
while (dataQueue.Peek() == null // nothing waiting yet
&& isBeingLoaded == true // and worker 1 still actively adding stuff
)
System.Threading.Thread.Sleep(100);
// otherwise ready to do something:
if (dataQueue.Peek() != null) // because maybe the queue is complete and also empty
{
string companyId = dataQueue.Dequeue();
processLists(companyId);
// use up the stuff here //
} // otherwise nothing was there yet, it will resolve on the next loop.
} while (isBeingLoaded == true // still have stuff coming at us
|| dataQueue.Peek() != null); // still have stuff we haven’t done
不過,我想用我應該使用一個線程ConcurrentQueue
打交道時。 我想知道如何在上面的Do While Loop中使用ConcurrentQueue
的例子嗎?
一切我與TryPeek是行不通的嘗試..
任何想法?
告訴我們:
,因爲你需要你可以旋轉起來成爲衆多消費者更多關於併發情況的信息:什麼線程正在運行您的發佈代碼?其他線程在做什麼?哪些線程寫入哪些變量? – Cameron 2013-02-20 22:27:49
您使用1個生產者和1個消費者嗎? – user7116 2013-02-20 22:28:12
「我用TryPeek試過的所有東西都不起作用。」它不是以什麼方式工作? – 2013-02-20 22:29:36