private void OptimizeTaskThreads() { optimizeStateList.Clear(); optimizeSleepTimeList.Clear(); optimizeCycleTimeList.Clear(); optimizeQueueCountList.Clear(); optimizationCache.Clear(); lock (optimalTaskThreadList) { optimalTaskThreadList.Clear(); int taskCount; lock (taskThreads) { taskCount = taskThreads.Count; foreach (TaskThread thread in taskThreads) { if (thread.IsEmpty) { optimalTaskThreadList.Add(float.MinValue, thread); continue; } optimizeStateList.Add(thread.State, thread); optimizeSleepTimeList.Add(thread.LastSleepTime, thread); optimizeCycleTimeList.Add(thread.AverageCycleTime, thread); optimizeQueueCountList.Add(thread.TasksQueued, thread); optimizationCache.Add(new Tuple <float, TaskThread>(0, thread)); } } int optimalCacheSize = optimizationCache.Count; for (int i = 0; i < optimalCacheSize; i++) { Tuple <float, TaskThread> op_info = optimizationCache[i];; TaskThread taskThread = op_info.Item2; float stateScore = (float)optimizeStateList.IndexOfValue(taskThread) * stateListWeight; float sleepScore = (float)optimizeSleepTimeList.IndexOfValue(taskThread) * stateListWeight; float cycleScore = (float)optimizeCycleTimeList.IndexOfValue(taskThread) * cycleListWeight; float queueCountScore = (float)optimizeSleepTimeList.IndexOfValue(taskThread) * stateListWeight; optimizationCache[i] = new Tuple <float, TaskThread>(stateScore + cycleScore + sleepScore + queueCountScore, taskThread); } foreach (Tuple <float, TaskThread> op_info in optimizationCache) { optimalTaskThreadList.Add(op_info.Item1, op_info.Item2); } } }
private void Setup(int threadCount) { lock (taskThreads) { for (int i = 0; i < threadCount; i++) { TaskThread thread = new TaskThread(this); thread.Start(); taskThreads.Add(thread); } } managerThread = new Thread(RunManager); managerThread.Start(); }