大家好,欢迎来到IT知识分享网。计算和计算所需的数据能被较为平均的非配到若干task的时候,下面的任务管理类可以提供在大数据大计算量的情况下非精确的控制task的执行数量来限制计算量和内存占用量.
下面是代码(非线程安全版本):
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());
}
}
线程安全版本:
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());
}
}
使用示例:
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
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/32068.html