Multi-task management class MutilTaskManager

Source: Internet
Author: User
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
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.