public List <Copy> ExecuteQuery() { if (ThreadCount == 0) { throw new InvalidOperationException("Threads property not set and default value 0 is not valid."); } //CODE HERE MergeSortTreeNodeJob RootJob = new MergeSortTreeNodeJob(ThreadCount, 0, this.Copies.Count - 1, 0, Copies); RootJob.RunMainJob(); return(RootJob.Result); }
public void RunMainJob() { if (this.NumThreads <= 0) { throw new ImTotallyStupidException("Some mistake occured while recounting numbers of threads, its <= 0"); } if (this.NumThreads == 1) { this.Result = FilterAndSort(); return; } //recount indeces and nums of threads, span new threads and wait fot them int numThreads1 = this.NumThreads / 2; //second part of a tree might contain one more thread then the firts one int numThreads2 = (this.NumThreads / 2) + (this.NumThreads % 2); int numElements = (this.EndIndex - this.StartIndex) + 1; int start1 = this.StartIndex; //and it might also be responsible for sorting one more element int end1 = (numElements / 2) - 1 + this.StartIndex; int start2 = end1 + 1; int end2 = this.EndIndex; MergeSortTreeNodeJob JobNode1 = new MergeSortTreeNodeJob(numThreads1, start1, end1, (this.levelInTree + 1), this.Data); Console.WriteLine($"The first thread of level {JobNode1.levelInTree} sorts {start1} - {end1}"); MergeSortTreeNodeJob JobNode2 = new MergeSortTreeNodeJob(numThreads2, start2, end2, (this.levelInTree + 1), this.Data); Console.WriteLine($"The second thread of level {JobNode2.levelInTree} sorts {start2} - {end2}"); Thread thread1 = new Thread(JobNode1.RunMainJob); thread1.Name = "Thread of the level " + JobNode1.levelInTree; Thread thread2 = new Thread(JobNode2.RunMainJob); thread2.Name = "Thread of the level " + JobNode2.levelInTree; thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); //MERGE Console.WriteLine($"{Thread.CurrentThread.Name} finished waiting for {thread1.Name} and {thread2.Name}"); Console.WriteLine("Merging"); List <Copy> merged = Merge(JobNode1.Result, JobNode2.Result); this.Result = merged; return; }