Example #1
0
        private void NotifyThreadPoolOfPendingWork()
        {
            if (_nowRunningCount < this.MaxConcurrentCount)
            {
                var canRunCount  = this.MaxConcurrentCount - _nowRunningCount;
                var failPopTimes = 0;
                while (_waitingTasks.Count > 0 && _idleThreads.Count > 0 && failPopTimes < _maxTryPopFailTimes)
                {
                    CThread worker;
                    if (!_idleThreads.TryDequeue(out worker))
                    {
                        failPopTimes++;
                        continue;
                    }

                    if (worker.IsShutdown)
                    {
                        _threads.Remove(worker);
                        //Interlocked.Decrement(ref _threadCount);
                        continue;
                    }

                    IDo task;
                    if (_waitingTasks.TryDequeue(out task))
                    {
                        if (task.IsCanDo(this.WorkItemTimeoutMiliseconds))
                        {
                            worker.DoWork(task);
                            Interlocked.Increment(ref _nowRunningCount);
                        }
                        else
                        {
                            Interlocked.Increment(ref _timeoutCount);
                        }
                    }
                }


                while (_waitingTasks.Count > 0 && _nowRunningCount < this.MaxConcurrentCount)
                {
                    IDo task;
                    if (_waitingTasks.TryDequeue(out task))
                    {
                        if (task.IsCanDo(this.WorkItemTimeoutMiliseconds))
                        {
                            CThread thread = new CThread(OnWorkComplete);
                            thread.DoWork(task);
                            Interlocked.Increment(ref _nowRunningCount);
                            //Interlocked.Increment(ref _threadCount);
                            //_threads[_threadCount - 1] = thread;
                            _threads.Add(thread);
                        }
                    }
                }
            }
        }
Example #2
0
        private void ResizeThreadPool(List <CThread> _threads, int _MaxConcurrentCount)
        {
            _threads.RemoveAll(x => x.IsShutdown);

            var diff = _threads.Count - _MaxConcurrentCount;

            if (diff > 0)
            {
                var idleThreads = _threads.Where(x => x.IsIdle && x.ThreadID > 0).ToArray();
                var idleDiff    = idleThreads.Count() - diff;

                if (idleDiff >= 0)
                {
                    foreach (var item in idleThreads)
                    {
                        item.Shutdown();
                        _threads.Remove(item);
                    }

                    _idleThreads = new ConcurrentQueue <CThread>();
                    if (idleDiff > 0)
                    {
                        var needShutdownThread = _threads.Take(idleDiff).ToArray();
                        var ids = new int[needShutdownThread.Length];
                        for (int i = 0; i < needShutdownThread.Length; i++)
                        {
                            needShutdownThread[i].Shutdown();
                            ids[0] = needShutdownThread[i].ThreadID;
                        }
                        _threads.RemoveAll(x => ids.Contains(x.ThreadID));
                    }
                }
                else
                {
                    for (int i = 0; i < -idleDiff && _threads.Count > 0; i++)
                    {
                        CThread thread = _threads[0];
                        thread.Shutdown();
                        _threads.Remove(thread);
                    }
                }
            }
        }
Example #3
0
        private static void ResizeThreadPool(ref CThread[] array, int newSize)
        {
            if (newSize < 0)
            {
                return;
            }

            CThread[] larray = array;
            if (larray == null)
            {
                array = new CThread[newSize];
                return;
            }

            if (larray.Length != newSize)
            {
                CThread[] newArray = new CThread[newSize];

                if (larray.Length < newSize)
                {
                    Array.Copy(larray, 0, newArray, 0, larray.Length > newSize ? newSize : larray.Length);
                }
                else
                {
                    var tmp = larray.Where(x => x != null && !x.IsShutdown).OrderBy(x => !x.IsIdle).ToArray();
                    Array.Copy(tmp, 0, newArray, 0, tmp.Length > newSize ? newSize : tmp.Length);
                    if (tmp.Length > newSize)
                    {
                        foreach (var t in tmp.Skip(newSize).ToArray())
                        {
                            t.Shutdown();
                        }
                    }
                }
                array = newArray;
            }
        }