static void Zad5(int table_size, int size_thread) { sum = 0; int[] tab = new int[table_size]; Random rnd = new Random(); Console.Write("Tablica: "); int tmp = 0; for (int i = 0; i < table_size; i++) { tab[i] = rnd.Next(1, 100); Console.Write(tab[i] + ", "); tmp += tab[i]; } Console.Write("\nSuma = " + tmp); for (int i = 0; i < table_size / size_thread; i++) { int[] tab_thread = new int[size_thread]; int k = 0; Console.Write("\nTablica Watku " + i + ": "); for (int j = (i * size_thread); j < (i * size_thread + size_thread); j++) { tab_thread[k] = tab[j]; Console.Write(tab_thread[k] + ", "); k++; } SumTab sb = new SumTab(i, tab_thread, size_thread); ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadSum), sb); } Console.Write("\nSum = " + sum); }
static void ThreadSum(Object stateInfo) { int tmp = 0; SumTab sb = (SumTab)stateInfo; for (int i = 0; i < sb.size; i++) { tmp += sb.tab[i]; } sum += tmp; Console.Write("\nWątek " + sb.numberThread + " sum = " + sum); }