Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TaskManager
- {
- private int currentCount = 0;
- public void Run()
- {
- Console.WriteLine("Starting Task manager");
- Task task = Task.Factory.StartNew(()=>SampleTask(GetCounter()));
- Task.WaitAll(new[] {task});
- }
- private void SampleTask(int taskId)
- {
- Console.WriteLine("Task {0} started", taskId);
- Random random = new Random();
- DoAckermannFunction(3,10);
- int count = random.Next(4);
- Console.WriteLine("Task {0} generate {1} new tasks", taskId, count);
- for (int i = 0; i < count; i++)
- {
- Task.Factory.StartNew(() => SampleTask(GetCounter()));
- }
- Console.WriteLine("Task {0} finished", taskId);
- }
- //http://en.wikipedia.org/wiki/Ackermann_function
- public static int DoAckermannFunction(int m, int n)
- {
- if (m == 0)
- return n + 1;
- if (m > 0 && n == 0)
- {
- return DoAckermannFunction(m - 1, 1);
- }
- if (m > 0 && n > 0)
- {
- return DoAckermannFunction(m - 1, DoAckermannFunction(m, n - 1));
- }
- return 0;
- }
- private int GetCounter()
- {
- lock (this)
- {
- int count = currentCount;
- currentCount++;
- return count;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement