public MultiThreadedParallelTaskCollection(int numberOfThreads = MAX_CONCURRENT_TASKS, bool relaxed = false)
        {
            _runners       = new MultiThreadRunner[numberOfThreads];
            _taskRoutines  = new ITaskRoutine[numberOfThreads];
            _parallelTasks = new ParallelTaskCollection[numberOfThreads];

            //prepare a single multithread runner for each group of fiber like task collections
            //number of threads can be less than the number of tasks to run
            for (int i = 0; i < numberOfThreads; i++)
            {
                _runners[i] = new MultiThreadRunner("MultiThreadedParallelTask:".FastConcat(i), relaxed);
            }

            //prepare the fiber-like paralleltasks
            for (int i = 0; i < numberOfThreads; i++)
            {
                var ptask = TaskRunner.Instance.AllocateNewTaskRoutine();
                var ptc   = new ParallelTaskCollection();
                ptc.onComplete += DecrementConcurrentOperationsCounter;

                ptask.SetEnumerator(ptc).SetScheduler(_runners[i]);

                _parallelTasks[i] = ptc;
                _taskRoutines[i]  = ptask;
                //once they are all done, the process will be completed
            }
        }
	// Use this for initialization
	void Start () 
	{
		ParallelTaskCollection pt = new ParallelTaskCollection();
		SerialTaskCollection	st = new SerialTaskCollection();
		
		st.Add(Print("s1"));
		st.Add(Print("s2"));
		st.Add(DoSomethingAsynchonously());
		st.Add(Print("s3"));
		st.Add(Print("s4"));
		
		pt.Add(Print("1"));
		pt.Add(Print("2"));
		pt.Add(WWWTest("www.google.com"));
		pt.Add(WWWTest("http://download.thinkbroadband.com/5MB.zip"));
		pt.Add(WWWTest("www.ebay.com"));
		pt.Add(Print("3"));
		pt.Add(Print("4"));
		pt.Add(st);
		pt.Add(Print("5"));
		pt.Add(Print("6"));
		pt.Add(Print("7"));
			
		TaskRunner.Instance.Run(pt.GetEnumerator());
	}
        /// <summary>
        /// Add can be called by another thread, so if the collection is already running
        /// I can't allow adding more tasks.
        /// </summary>
        /// <param name="enumerator"></param>
        /// <exception cref="MultiThreadedParallelTaskCollectionException"></exception>
        public void Add(IEnumerator enumerator)
        {
            if (isRunning == true)
            {
                throw new MultiThreadedParallelTaskCollectionException("can't add tasks on a started MultiThreadedParallelTaskCollection");
            }

            ParallelTaskCollection parallelTaskCollection = _parallelTasks[_numberOfTasksAdded++ % _parallelTasks.Length];

            parallelTaskCollection.Add(enumerator);

            _numberOfConcurrentOperationsToRun = Math.Min(_parallelTasks.Length, _numberOfTasksAdded);
        }
        public void Setup()
        {
            serialTasks1 = new SerialTaskCollection();
            parallelTasks1 = new ParallelTaskCollection();
            serialTasks2 = new SerialTaskCollection();
            parallelTasks2 = new ParallelTaskCollection();

            task1 = new Task(15);
            task2 = new Task(5);

            iterable1 = new Enumerable(15);
            iterable2 = new Enumerable(5);

            iterations = 0;

            _taskRunner = TaskRunner.Instance;
        }
        void InitializeThreadsAndData(uint numberOfThreads, bool runningTightTasks)
        {
            _runners       = new MultiThreadRunner[numberOfThreads];
            _taskRoutines  = new ITaskRoutine[numberOfThreads];
            _parallelTasks = new ParallelTaskCollection[numberOfThreads];

            //prepare a single multithread runner for each group of fiber like task collections
            //number of threads can be less than the number of tasks to run
            for (int i = 0; i < numberOfThreads; i++)
            {
                _runners[i] = new MultiThreadRunner("MultiThreadedParallelTask #".FastConcat(i), runningTightTasks);
            }

            Action ptcOnOnComplete = DecrementConcurrentOperationsCounter;
            Func <Exception, bool> ptcOnOnException = (e) =>
            {
                DecrementConcurrentOperationsCounter();
                return(false);
            };


            //prepare the fiber-like paralleltasks
            for (int i = 0; i < numberOfThreads; i++)
            {
                var ptask = TaskRunner.Instance.AllocateNewTaskRoutine();
                var ptc   = new ParallelTaskCollection("ParallelTaskCollection #".FastConcat(i));

                ptc.onComplete  += ptcOnOnComplete;
                ptc.onException += ptcOnOnException;

                ptask.SetEnumerator(ptc).SetScheduler(_runners[i]);

                _parallelTasks[i] = ptc;
                _taskRoutines[i]  = ptask;
            }
        }
	// Use this for initialization
	void Start () 
	{
		Application.targetFrameRate = 20;
		
		ParallelTaskCollection pt = new ParallelTaskCollection();
		SerialTaskCollection	st = new SerialTaskCollection();
		
		st.Add(Print("s1"));
		st.Add(DoSomethingAsynchonously());
		st.Add(Print("s3"));
		
		pt.Add(Print("1"));
		pt.Add(Print("2"));
		pt.Add(Print("3"));
		pt.Add(Print("4"));
		pt.Add(Print("5"));
		pt.Add(st);
		pt.Add(Print("6"));
		pt.Add(WWWTest ());
		pt.Add(Print("7"));
		pt.Add(Print("8"));
			
		StartCoroutine(pt.GetEnumerator());
	}
 public ParallelTask(ParallelTaskCollection parent)
 {
     _parent = parent;
 }