Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.IO;
- namespace Pb10
- {
- class Program
- {
- static object countLock = new object();
- private const string FILE_NAME="suma.txt";
- static void Main(string[] args)
- {
- using (File.Create(FILE_NAME));
- int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- while (array.Length / 2 >= 1)
- {
- int[] aux;
- //daca dimensiunea este impara adaugam un 0 la sfarsit
- if (array.Length % 2 != 0)
- {
- aux = array;
- array = new int[aux.Length + 1];
- for (int i = 0; i < aux.Length; i++)
- {
- array[i] = aux[i];
- }
- array[aux.Length] = 0;
- }
- for (int i = 0; i < array.Length / 2; i++)
- {
- Thread t = new Thread(delegate()
- {
- int c = array[i * 2] + array[i * 2 + 1];
- string lines = c + " ";
- array[i] = c;
- //folosit pentru accesul concurent la obiecte
- Monitor.Enter(countLock);
- //using se asigura ca Dispose este apelata chiar daca se arunca o exceptie asupra metodelor din obiect
- using (System.IO.StreamWriter file = File.AppendText(FILE_NAME))
- {
- file.WriteLine(lines);
- }
- Monitor.Exit(countLock);
- });
- t.Start();
- t.Join();
- }
- aux = array;
- array = new int[array.Length / 2];
- for (int i = 0; i < array.Length; i++)
- {
- array[i] = aux[i];
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement