コード例 #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
ファイル: CThreadPool.cs プロジェクト: vebin/AntServiceStack
        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
 public bool DoWork(ICWorkItem task)
 {
     if (!this._isIdle || (this._task != null))
     {
         return(false);
     }
     this._task = task;
     if (!this._isStart)
     {
         this._isStart = true;
         this._thread.Start();
     }
     else
     {
         this._newJobWaitHandle.Set();
     }
     return(true);
 }
コード例 #4
0
        void DoTask()
        {
            while (!_isShutdown)
            {
                _isIdle = false;

                try
                {
                    while (_task != null)
                    {
                        ICWorkItem task;
                        lock (_task)
                        {
                            task = _task;

                            task.Do();
                        }

                        _task = null;

                        _isIdle = true;
                        _completeCallback(this, task);
                    }
                }catch (Exception ex) {
                    CHystrix.Utils.CommonUtils.Log.Log(LogLevelEnum.Error, ex.Message, ex);
                }

                if (!_isShutdown)
                {
                    _newJobWaitHandle.Reset();

                    if (!_newJobWaitHandle.WaitOne(ThreadMaxIdleTime) && _task == null)
                    {
                        _isShutdown = true;
                        _isIdle     = false;
                        _thread     = null;
                    }
                }
            }
        }
コード例 #5
0
        public bool DoWork(ICWorkItem task)
        {
            if (_isIdle && null == _task)
            {
                _task = task;
                if (!_isStart)
                {
                    _isStart = true;
                    _thread.Start();
                }
                else
                {
                    _newJobWaitHandle.Set();
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #6
0
 private void DoTask()
 {
     while (!this._isShutdown)
     {
         this._isIdle = false;
         try
         {
             while (this._task != null)
             {
                 ICWorkItem item;
                 lock (this._task)
                 {
                     item = this._task;
                     item.Do();
                 }
                 this._task   = null;
                 this._isIdle = true;
                 this._completeCallback(this, item);
             }
         }
         catch (Exception exception)
         {
             CommonUtils.Log.Log(LogLevelEnum.Error, exception.Message, exception);
         }
         if (!this._isShutdown)
         {
             this._newJobWaitHandle.Reset();
             if (!this._newJobWaitHandle.WaitOne(ThreadMaxIdleTime) && (this._task == null))
             {
                 this._isShutdown = true;
                 this._isIdle     = false;
                 this._thread     = null;
             }
         }
     }
 }