When the data required for computing and computing is evenly distributed to several tasks, the following task management class can provide inaccurate control of the number of tasks in the case of big data computing to limit the computing amount and memory usage.
The following is the code (non-thread-safe version ):
Public class MutilTaskManager
{
Private readonly int _ maxRun;
Private readonly int _ maxQuenen;
Private List <Task> _ taskRunningList;
Private readonly Queue <Task> _ taskQueue;
Public bool IsQueueFull
{
Get {return _ taskQueue. Count> _ maxQuenen ;}
}
Public MutilTaskManager (int maxRun = 8, int maxQuenen = 2)
{
_ MaxRun = maxRun;
_ MaxQuenen = maxQuenen;
_ TaskRunningList = new List <Task> (maxRun );
_ TaskQueue = new Queue <Task> (2 );
}
Public void TakeBooting (int sleepTime = 10)
{
If (_ taskRunningList. Count> = _ maxRun)
{
_ TaskRunningList = _ taskRunningList. Where (it =>! It. IsCompleted). ToList ();
}
While (_ taskRunningList. Count <_ maxRun & _ taskQueue. Count> 0)
{
Var t = _ taskQueue. Dequeue ();
_ TaskRunningList. Add (t );
T. Start ();
}
System. Threading. Thread. Sleep (sleepTime );
}
Public void Enqueue (Task task)
{
_ TaskQueue. Enqueue (task );
TakeBooting (0 );
}
Public void WaitAll ()
{
While (_ taskQueue. Count> 0)
{
TakeBooting ();
}
Task. WaitAll (_ taskRunningList. ToArray ());
}
}
Thread Security version:
Public class MutilTaskManager
{
Private readonly int _ maxRun;
Private readonly int _ maxQuenen;
Private List <Task> _ taskRunningList;
Private readonly Queue <Task> _ taskQueue;
Private object _ lockObj = new object ();
Public bool IsQueueFull
{
Get {return _ taskQueue. Count> _ maxQuenen ;}
}
Public MutilTaskManager (int maxRun = 8, int maxQuenen = 2)
{
_ MaxRun = maxRun;
_ MaxQuenen = maxQuenen;
_ TaskRunningList = new List <Task> (maxRun );
_ TaskQueue = new Queue <Task> (2 );
}
Public void TakeBooting (int sleepTime = 10)
{
Lock (_ lockObj)
{
If (_ taskRunningList. Count> = _ maxRun)
{
_ TaskRunningList = _ taskRunningList. Where (it =>! It. IsCompleted). ToList ();
}
While (_ taskRunningList. Count <_ maxRun & _ taskQueue. Count> 0)
{
Var t = _ taskQueue. Dequeue ();
_ TaskRunningList. Add (t );
T. Start ();
}
}
System. Threading. Thread. Sleep (sleepTime );
}
Public void Enqueue (Task task)
{
Lock (_ lockObj)
{
_ TaskQueue. Enqueue (task );
}
TakeBooting (0 );
}
Public void WaitAll ()
{
While (_ taskQueue. Count> 0)
{
TakeBooting ();
}
Task. WaitAll (_ taskRunningList. ToArray ());
}
}
Example:
Void Test ()
{
Var mtm = new MutilTaskManager ();
Foreach (int I in (new int [10])
{
While (mtm. IsQueueFull)
{
Mtm. TakeBooting ();
}
Mtm. Enqueue (new Task (myTask ));
}
}
Void myTask ()
{
// Read data
// Do something
}