static void SaveResults(ResultsMonitor resultsMonitor) { using (StreamWriter sr = new StreamWriter("(res)IFF_8-10_LauzadisR_L1_res.txt")) { sr.WriteLine(" Name| Exams| Min Grade"); sr.WriteLine("------------------------------------------------"); foreach (Student student in resultsMonitor.GetData()) { sr.WriteLine("{0,14}|{1,16}|{2,16}", student.Name, student.Exams, student.Grade); } } }
static void ThreadWorker(ref DataMonitor <Student> dataMonitor, ref ResultsMonitor resultsMonitor) { Student student; while ((student = (Student)dataMonitor.GetLastElement()) != null) { if (IsValid(student)) { resultsMonitor.AddElement(student); } } Console.WriteLine("One thread finished"); return; }
static void Main(string[] args) { //Nuskaitomi duomenys iš failų List <Student> students = ReadData(); List <Thread> threads = new List <Thread>(); ResultsMonitor resultsMonitor = new ResultsMonitor(); DataMonitor <Student> dataMonitor = new DataMonitor <Student>(students.Count); //Paleidžiamos darbininkių gijos, kurios ims duomenis iš duomenų monitoriaus for (int i = 0; i < 20; i++) { threads.Add(new Thread(() => ThreadWorker(ref dataMonitor, ref resultsMonitor))); } threads.ForEach(thread => { thread.Start(); }); //Pagrindinė gija deda elementus į duomenų monitorių students.ForEach(x => { dataMonitor.AddElement(x); }); threads.ForEach(thread => { thread.Join(); }); Console.WriteLine("Results: " + resultsMonitor); SaveResults(resultsMonitor); Console.ReadKey(); }