fork download
  1. using System;
  2. using System.Collections.Generic;
  3. //using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace A
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. var filesCount = 14;
  14. var threads = 5;
  15. int withoutResidue = filesCount / threads;
  16. int residue = filesCount % threads;
  17. Thread[] threadArray = new Thread[threads];
  18.  
  19. for (int i = 0; i < (threadArray = new Thread[threads]).Length; i++)
  20. {
  21. threadArray[i] = new Thread(new ThreadStart(() =>
  22. {
  23. Console.WriteLine("x {0}", i);
  24. Thread.Sleep(100);
  25.  
  26. for (int j = 0; j < withoutResidue; j++)
  27. {
  28. Console.WriteLine("+ {0}", i);
  29. Thread.Sleep(100);
  30. }
  31.  
  32. if (residue != 0)
  33. {
  34. Console.WriteLine("- {0}", i);
  35. Thread.Sleep(100);
  36. }
  37. }
  38. )) { IsBackground = true };
  39. threadArray[i].Start(i);
  40. }
  41.  
  42. Thread.Sleep(3000);
  43. }
  44. }
  45. }
Success #stdin #stdout 0.03s 39064KB
stdin
Standard input is empty
stdout
x 0
x 1
x 2
x 3
x 4
+ 5
+ 5
+ 5
+ 5
+ 5
+ 5
+ 5
+ 5
+ 5
+ 5
- 5
- 5
- 5
- 5
- 5