Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.IO;
  7. namespace Pb10
  8. {
  9. class Program
  10. {
  11. static object countLock = new object();
  12. private const string FILE_NAME="suma.txt";
  13. static void Main(string[] args)
  14. {
  15. using (File.Create(FILE_NAME));
  16. int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  17. while (array.Length / 2 >= 1)
  18. {
  19. int[] aux;
  20. //daca dimensiunea este impara adaugam un 0 la sfarsit
  21. if (array.Length % 2 != 0)
  22. {
  23. aux = array;
  24. array = new int[aux.Length + 1];
  25. for (int i = 0; i < aux.Length; i++)
  26. {
  27. array[i] = aux[i];
  28. }
  29. array[aux.Length] = 0;
  30. }
  31.  
  32. for (int i = 0; i < array.Length / 2; i++)
  33. {
  34. Thread t = new Thread(delegate()
  35. {
  36. int c = array[i * 2] + array[i * 2 + 1];
  37. string lines = c + " ";
  38. array[i] = c;
  39. //folosit pentru accesul concurent la obiecte
  40. Monitor.Enter(countLock);
  41. //using se asigura ca Dispose este apelata chiar daca se arunca o exceptie asupra metodelor din obiect
  42. using (System.IO.StreamWriter file = File.AppendText(FILE_NAME))
  43. {
  44. file.WriteLine(lines);
  45. }
  46. Monitor.Exit(countLock);
  47. });
  48. t.Start();
  49. t.Join();
  50. }
  51. aux = array;
  52. array = new int[array.Length / 2];
  53. for (int i = 0; i < array.Length; i++)
  54. {
  55. array[i] = aux[i];
  56. }
  57. }
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement