Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. public class TaskManager
  2.     {
  3.         private int currentCount = 0;
  4.  
  5.         public void Run()
  6.         {
  7.             Console.WriteLine("Starting Task manager");
  8.             Task task = Task.Factory.StartNew(()=>SampleTask(GetCounter()));
  9.             Task.WaitAll(new[] {task});
  10.  
  11.         }
  12.  
  13.  
  14.         private void SampleTask(int taskId)
  15.         {
  16.             Console.WriteLine("Task {0} started", taskId);
  17.             Random random = new Random();
  18.  
  19.             DoAckermannFunction(3,10);
  20.  
  21.             int count = random.Next(4);
  22.             Console.WriteLine("Task {0} generate {1} new tasks", taskId, count);
  23.             for (int i = 0; i < count; i++)
  24.             {
  25.                 Task.Factory.StartNew(() => SampleTask(GetCounter()));
  26.             }
  27.             Console.WriteLine("Task {0} finished", taskId);
  28.         }
  29.  
  30.         //http://en.wikipedia.org/wiki/Ackermann_function
  31.         public static int DoAckermannFunction(int m, int n)
  32.         {
  33.             if (m == 0)
  34.                 return n + 1;
  35.             if (m > 0 && n == 0)
  36.             {
  37.                 return DoAckermannFunction(m - 1, 1);
  38.             }
  39.             if (m > 0 && n > 0)
  40.             {
  41.                 return DoAckermannFunction(m - 1, DoAckermannFunction(m, n - 1));
  42.             }
  43.             return 0;
  44.         }
  45.  
  46.         private int GetCounter()
  47.         {
  48.             lock (this)
  49.             {
  50.                 int count = currentCount;
  51.                 currentCount++;
  52.                 return count;
  53.             }
  54.         }
  55.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement