예제 #1
0
        private void OnWorkComplete(CThread thread, ICWorkItem task)
        {
            Interlocked.Increment(ref _finishedCount);

            ICWorkItem newTask;
            bool       hasWorkDo = false;

            try
            {
                if (_ThreadWorkCompleteCallback != null)
                {
                    _ThreadWorkCompleteCallback(task);
                }
            }
            catch (Exception ex)
            {
                CHystrix.Utils.CommonUtils.Log.Log(LogLevelEnum.Error, ex.Message, ex);
            }

            if (_nowRunningCount + _idleThreads.Count > _MaxConcurrentCount)
            {
                thread.Shutdown();
            }

            if (!thread.IsShutdown)
            {
                while (_waitingTasks.TryDequeue(out newTask))
                {
                    if (newTask.CanDo(this.WorkItemTimeoutMiliseconds))
                    {
                        thread.DoWork(newTask);
                        hasWorkDo = true;
                        break;
                    }
                    else if (newTask.MarkTimeout())
                    {
                        Interlocked.Increment(ref _timeoutCount);
                    }
                }
            }

            if (!hasWorkDo)
            {
                CThread tmp;
                if (_threads.TryRemove(thread.ThreadID, out tmp))
                {
                    Interlocked.Decrement(ref _nowRunningCount);
                    _idleThreads.Enqueue(thread);
                }
            }
        }
예제 #2
0
        private void OnWorkComplete(CThread thread, ICWorkItem task)
        {
            CThread thread2;

            Interlocked.Increment(ref this._finishedCount);
            bool flag = false;

            try
            {
                if (this._ThreadWorkCompleteCallback != null)
                {
                    this._ThreadWorkCompleteCallback(task);
                }
            }
            catch (Exception exception)
            {
                CommonUtils.Log.Log(LogLevelEnum.Error, exception.Message, exception);
            }
            if ((this._nowRunningCount + this._idleThreads.Count) > this._MaxConcurrentCount)
            {
                thread.Shutdown();
            }
            if (!thread.IsShutdown)
            {
                ICWorkItem item;
                while (this._waitingTasks.TryDequeue(out item))
                {
                    if (item.CanDo(this.WorkItemTimeoutMiliseconds))
                    {
                        thread.DoWork(item);
                        flag = true;
                        break;
                    }
                    if (item.MarkTimeout())
                    {
                        Interlocked.Increment(ref this._timeoutCount);
                    }
                }
            }
            if (!flag && this._threads.TryRemove(thread.ThreadID, out thread2))
            {
                Interlocked.Decrement(ref this._nowRunningCount);
                this._idleThreads.Enqueue(thread);
            }
        }
예제 #3
0
        private void NotifyThreadPoolOfPendingWork()
        {
            if (_idleThreads.Count == 0 && _nowRunningCount >= this.MaxConcurrentCount)
            {
                return;
            }

            var failPopTimes = 0;

            while (_waitingTasks.Count > 0 && _idleThreads.Count > 0 && failPopTimes < _maxTryPopFailTimes)
            {
                CThread worker;

                if (!_idleThreads.TryDequeue(out worker))
                {
                    Thread.Sleep(10);
                    failPopTimes++;
                    continue;
                }

                if (_nowRunningCount + _idleThreads.Count > this.MaxConcurrentCount)
                {
                    worker.Shutdown();
                    continue;
                }

                if (worker.IsShutdown)
                {
                    continue;
                }

                ICWorkItem task;
                var        hasWorkDo = false;
                while (_waitingTasks.TryDequeue(out task))
                {
                    if (task.CanDo(this.WorkItemTimeoutMiliseconds))
                    {
                        worker.DoWork(task);
                        hasWorkDo = true;
                        Interlocked.Increment(ref _nowRunningCount);

                        _threads.TryAdd(worker.ThreadID, worker);

                        break;
                    }
                    else if (task.MarkTimeout())
                    {
                        Interlocked.Increment(ref _timeoutCount);
                    }
                }

                if (!hasWorkDo)
                {
                    _idleThreads.Enqueue(worker);
                    break;
                }
            }


            while (_waitingTasks.Count > 0 && _nowRunningCount + _idleThreads.Count < this.MaxConcurrentCount)
            {
                ICWorkItem task;
                if (_waitingTasks.TryDequeue(out task))
                {
                    if (task.CanDo(this.WorkItemTimeoutMiliseconds))
                    {
                        CThread thread = new CThread(OnWorkComplete);
                        thread.DoWork(task);
                        Interlocked.Increment(ref _nowRunningCount);

                        _threads.TryAdd(thread.ThreadID, thread);
                    }
                    else if (task.MarkTimeout())
                    {
                        Interlocked.Increment(ref _timeoutCount);
                    }
                }
            }
        }
예제 #4
0
 private void NotifyThreadPoolOfPendingWork()
 {
     if ((this._idleThreads.Count != 0) || (this._nowRunningCount < this.MaxConcurrentCount))
     {
         int num = 0;
         while (((this._waitingTasks.Count > 0) && (this._idleThreads.Count > 0)) && (num < 3))
         {
             CThread thread;
             if (!this._idleThreads.TryDequeue(out thread))
             {
                 Thread.Sleep(10);
                 num++;
                 continue;
             }
             if ((this._nowRunningCount + this._idleThreads.Count) > this.MaxConcurrentCount)
             {
                 thread.Shutdown();
                 continue;
             }
             if (!thread.IsShutdown)
             {
                 ICWorkItem item;
                 bool       flag = false;
                 while (this._waitingTasks.TryDequeue(out item))
                 {
                     if (item.CanDo(this.WorkItemTimeoutMiliseconds))
                     {
                         thread.DoWork(item);
                         flag = true;
                         Interlocked.Increment(ref this._nowRunningCount);
                         this._threads.TryAdd(thread.ThreadID, thread);
                         break;
                     }
                     if (item.MarkTimeout())
                     {
                         Interlocked.Increment(ref this._timeoutCount);
                     }
                 }
                 if (!flag)
                 {
                     this._idleThreads.Enqueue(thread);
                     break;
                 }
             }
         }
         while ((this._waitingTasks.Count > 0) && ((this._nowRunningCount + this._idleThreads.Count) < this.MaxConcurrentCount))
         {
             ICWorkItem item2;
             if (this._waitingTasks.TryDequeue(out item2))
             {
                 if (item2.CanDo(this.WorkItemTimeoutMiliseconds))
                 {
                     CThread thread2 = new CThread(new CThreadWorkCompleteCallback(this.OnWorkComplete));
                     thread2.DoWork(item2);
                     Interlocked.Increment(ref this._nowRunningCount);
                     this._threads.TryAdd(thread2.ThreadID, thread2);
                 }
                 else if (item2.MarkTimeout())
                 {
                     Interlocked.Increment(ref this._timeoutCount);
                 }
             }
         }
     }
 }