Пример #1
0
        /// <summary>
        /// Routes actions to the threads
        /// </summary>
        /// <param name="action"></param>
        private void route(Action action)
        {
            //check if there is a active thread without running actions if so add action
            BalanceThread nonBusyThread = searchForNonBusyThread();

            if (nonBusyThread != null)
            {
                nonBusyThread.addAction(action);
                return;
            }

            //We didnt found a busy thread so we are going to make a new one
            if (getThreadPoolCount() < _preferedPoolSize)
            {
                Utils.log("creating new BalanceThread in pool");
                _pool.Add(new BalanceThread(this, action)
                          .runInBackground(_preferedAsBackgroundThread)
                          .setSleepTime(_threadLifeTime)
                          .run());
                return;
            }
            else
            {   //thread limit is crossed add to queue
                int[] actionValues = new int[_pool.Count];

                for (int i = 0; i < _pool.Count; i++)
                {
                    //re-check if threads are busy and create a list of actions to determine the best Thread
                    BalanceThread thread = _pool[i];
                    if (thread.isBusy())
                    {
                        actionValues[i] = thread.getActionQueue();
                    }
                    else
                    {
                        thread.addAction(action);
                        Utils.log("adding new action to living non busy thread");
                    }
                }
                //check thread with lowest amount of action
                int minimalCount = actionValues.Min();
                for (int t = 0; t < actionValues.Length; t++)
                {
                    if (minimalCount == actionValues[t])
                    {
                        _pool[t].addAction(action);
                    }
                }
            }
        }
Пример #2
0
 /// <summary>
 /// Releases thread from pool
 /// </summary>
 /// <param name="thread"></param>
 public void release(BalanceThread thread)
 {
     Utils.log("thread released from pool");
     threadExitEvent();
     _pool.Remove(thread);
 }
Пример #3
0
 /// <summary>
 /// Called by a child BalanceThread when all the actions are done.
 /// Returns if the thread should be alive or not
 /// </summary>
 /// <returns></returns>
 public bool onEndResult(BalanceThread thread)
 {
     return(!(_pool.Count > _maxThreadCache));
 }