Пример #1
0
        /// <summary>
        /// 根据等待执行队列中,事件的权重,获取线程并且执行
        /// </summary>
        private void ThreadQueueHandle()
        {
            lock (curAliveThreadCountLock)
            {
                if (curAliveThreadCount >= maxThreadCount)
                {
                    return;
                }
            }
            ThreadMainHandle handler = null;
            string           id      = "";

            lock (weightQueue)
            {
                if (weightQueue.Count <= 0)
                {
                    return;
                }
                handler = weightQueue.DeQueue(out id);
            }
            if (handler != null)
            {
                ItemThread item = GetAItem();
                item.Start(id,
                           delegate()
                {
                    handler();
                });
                lock (aliveList)
                {
                    aliveList.Add(item);
                }
            }
        }
Пример #2
0
 /// <summary>
 /// 销毁一个线程
 /// </summary>
 /// <param name="item"></param>
 private void DestoryThread(ItemThread item)
 {
     lock (threadDestoryIds)
     {
         threadDestoryIds.Add(item.Id);
     }
 }
Пример #3
0
        /// <summary>
        /// 线程执行报错
        /// </summary>
        /// <param name="item"></param>
        private void ErrorThread(ItemThread item)
        {
            KeyValuePair <string, string> pair = new KeyValuePair <string, string>(item.Id, item.ErrorCode);

            lock (errorDic)
            {
                ErrorMessageHandle handler;
                //errorDic.Add(pair);
                if (errorDic.TryGetValue(item.Id, out handler))
                {
                    if (handler != null)
                    {
                        handler(item.ErrorCode);
                    }
                    errorDic.Remove(item.Id);
                }
                errorList.Add(pair);
            }
            lock (curAliveThreadCountLock)
                curAliveThreadCount--;
            lock (destoryObjLock)
                if (isDestory)
                {
                    item.Stop();
                    return;
                }
            ThreadQueueHandle();
        }
Пример #4
0
 /// <summary>
 /// 销毁线程池
 /// </summary>
 public void Destory()
 {
     lock (destoryObjLock)
         isDestory = true;
     lock (idleQueue)
     {
         for (int i = idleQueue.Count - 1; i >= 0; i--)
         {
             ItemThread item = idleQueue.Dequeue();
             item.Stop();
         }
     }
     lock (weightQueue)
         weightQueue.Clear();
 }
Пример #5
0
 /// <summary>
 /// 线程执行完毕,回归等待队列
 /// </summary>
 /// <param name="item"></param>
 private void EndThread(ItemThread item)
 {
     lock (aliveList)
         aliveList.Remove(item);
     lock (curAliveThreadCountLock)
         curAliveThreadCount--;
     lock (destoryObjLock)
         if (isDestory)
         {
             item.Stop();
             return;
         }
     lock (idleQueue)
         idleQueue.Enqueue(item);
     ThreadQueueHandle();
 }
Пример #6
0
        /// <summary>
        /// 获取一个线程
        /// </summary>
        /// <returns></returns>
        private ItemThread GetAItem()
        {
            lock (curAliveThreadCountLock)
            {
                if (curAliveThreadCount > maxThreadCount)
                {
                    return(null);
                }
                else
                {
                    curAliveThreadCount++;
                }
            }
            ItemThread item = GetAItemFromIdleQueue();

            if (item == null)
            {
                item = GenerateOneItem();
            }
            return(item);
        }
Пример #7
0
        /// <summary>
        /// 生成一个线程
        /// </summary>
        /// <returns></returns>
        private ItemThread GenerateOneItem()
        {
            ItemThread item = new ItemThread(DestoryThread, EndThread, ErrorThread);

            return(item);
        }