データベースからキューを読み取って処理するプログラムを作成する必要があり、すべてのキューは並行して実行され、ConcurrentDictionary を使用して親スレッドで管理されます。キューを表すクラスがあります。このクラスには、キュー情報と親インスタンス ハンドルを受け取るコンストラクターがあります。キュー クラスには、キューを処理するメソッドもあります。
キュー クラスは次のとおりです。
Class MyQueue {
protected ServiceExecution _parent;
protect string _queueID;
public MyQueue(ServiceExecution parentThread, string queueID)
{
_parent = parentThread;
_queueID = queueID;
}
public void Process()
{
try
{
//Do work to process
}
catch()
{
//exception handling
}
finally{
_parent.ThreadFinish(_queueID);
}
親スレッドはキューのデータセットをループし、新しいキュー クラスをインスタンス化します。Queue オブジェクトの Process メソッドを非同期的に実行する新しいスレッドを生成します。このスレッドは ConcurrentDictionary に追加され、次のように開始されます。
private ConcurrentDictionary<string, MyQueue> _runningQueues = new ConcurrentDictionary<string, MyQueue>();
Foreach(datarow dr in QueueDataset.rows)
{
MyQueue queue = new MyQueue(this, dr["QueueID"].ToString());
Thread t = new Thread(()=>queue.Process());
if(_runningQueues.TryAdd(dr["QueueID"].ToString(), queue)
{
t.start();
}
}
//Method that gets called by the queue thread when it finishes
public void ThreadFinish(string queueID)
{
MyQueue queue;
_runningQueues.TryRemove(queueID, out queue);
}
これは非同期キュー処理を管理するための正しいアプローチではないと感じています。この設計でデッドロックが発生する可能性があるのではないかと考えています。さらに、タスクを使用して、新しいスレッドの代わりにキューを非同期で実行したいと考えています。前の実行がまだ完了していない場合、同じキューに対して新しいスレッドまたはタスクを生成しないため、キューを追跡する必要があります。このタイプの並列処理を処理する最良の方法は何ですか?
前もって感謝します!