ソースを取得する必要がある URL のリストを含む ConcurrentQueue があります。ConcurrentQueue オブジェクトを入力パラメーターとして Parallel.ForEach を使用すると、Pop メソッドは何も機能しません (文字列を返す必要があります)。
MaxDegreeOfParallelism を 4 に設定して Parallel を使用しています。私は本当に同時スレッドの数をブロックする必要があります。並列処理でキューを使用するのは冗長ですか?
前もって感謝します。
// On the main class
var items = await engine.FetchPageWithNumberItems(result);
// Enqueue List of items
itemQueue.EnqueueList(items);
var crawl = Task.Run(() => { engine.CrawlItems(itemQueue); });
// On the Engine class
public void CrawlItems(ItemQueue itemQueue)
{
Parallel.ForEach(
itemQueue,
new ParallelOptions {MaxDegreeOfParallelism = 4},
item =>
{
var worker = new Worker();
// Pop doesn't return anything
worker.Url = itemQueue.Pop();
/* Some work */
});
}
// Item Queue
class ItemQueue : ConcurrentQueue<string>
{
private ConcurrentQueue<string> queue = new ConcurrentQueue<string>();
public string Pop()
{
string value = String.Empty;
if(this.queue.Count == 0)
throw new Exception();
this.queue.TryDequeue(out value);
return value;
}
public void Push(string item)
{
this.queue.Enqueue(item);
}
public void EnqueueList(List<string> list)
{
list.ForEach(this.queue.Enqueue);
}
}