Esempio n. 1
0
 /// <summary>
 /// 在通用线程池内加入待处理的方法
 /// </summary>
 /// <param name="handler">处理方法</param>
 /// <param name="context">上下文对象</param>
 public static void EnqueueHandle(ThreadBoxHandler handler, object context)
 {
     lock (commonThreadboxQueue)
         commonThreadboxQueue.Enqueue(new KeyValuePair <object, ThreadBoxHandler>(context, handler));
 }
Esempio n. 2
0
        public static void CreateQueueConsumerThreadBox <T>(string threadboxName, int coreThreadNum, int maxThreadNum, ThreadBoxHandler <T> handler, Queue <T> queue, int milliumsecondSleepTime = 100, bool isStopOnEmptyQueue = false)
        {
            if (threadboxName == "" || coreThreadNum <= 0 || maxThreadNum <= 0 || maxThreadNum < coreThreadNum || handler == null || queue == null)
            {
                throw new ArgumentException();
            }


            Box b = null;

            lock (dicBox)
            {
                if (dicBox.ContainsKey(threadboxName))
                {
                    throw new ArgumentException("threadboxName exist");
                }
                b = new Box(coreThreadNum, maxThreadNum);
                dicBox.Add(threadboxName, b);
            }
            QueueJob <T> j = new QueueJob <T>();

            j.isStop    = isStopOnEmptyQueue;
            j.sleepTime = milliumsecondSleepTime;
            j.queue     = queue;
            j.handler   = handler;
            //b.job = j;
            b.Start(j);
        }
Esempio n. 3
0
        /// <summary>
        /// 创建一个线程池去处理一个集合容器
        /// </summary>
        /// <typeparam name="T">队列容器中对象的类型</typeparam>
        /// <param name="threadboxName">线程池名称</param>
        /// <param name="threadNum">线程池线程数量</param>
        /// <param name="handler">处理方法</param>
        /// <param name="list">集合容器</param>
        /// <param name="milliumsecondSleepTime">当容器为空时休息时间(毫秒),默认为100毫秒</param>
        /// <param name="isStopOnEmptyQueue">是否当队列为空时才关闭,默认为FALSE</param>
        public static void CreateColletionConsumerThreadBox <T>(string threadboxName, int threadNum, ThreadBoxHandler <T> handler, ICollection <T> list, int milliumsecondSleepTime = 100, bool isStopOnEmptyQueue = false)
        {
            if (threadboxName == "" || threadNum <= 0 || handler == null || list == null)
            {
                throw new ArgumentException();
            }


            Box b = null;

            lock (dicBox)
            {
                if (dicBox.ContainsKey(threadboxName))
                {
                    throw new ArgumentException("threadboxName exist");
                }
                b = new Box(threadNum);
                dicBox.Add(threadboxName, b);
            }

            ListJob <T> j = new ListJob <T>();

            j.isStop    = isStopOnEmptyQueue;
            j.sleepTime = milliumsecondSleepTime;
            j.list      = list;
            j.handler   = handler;
            b.Start(j);
        }
Esempio n. 4
0
        public static void CreateQueueConsumerThreadBox <T>(string threadboxName, int threadNum, ThreadBoxHandler <T> handler, PopItemMethod <T> method, int milliumsecondSleepTime = 100, bool isStopOnEmptyQueue = false)
        {
            if (threadboxName == "" || threadNum <= 0 || handler == null || method == null)
            {
                throw new ArgumentException();
            }


            Box b = null;

            lock (dicBox)
            {
                if (dicBox.ContainsKey(threadboxName))
                {
                    throw new ArgumentException("threadboxName exist");
                }
                b = new Box(threadNum);
                dicBox.Add(threadboxName, b);
            }
            MethodJob <T> j = new MethodJob <T>();

            j.isStop    = isStopOnEmptyQueue;
            j.sleepTime = milliumsecondSleepTime;
            j.method    = method;
            j.handler   = handler;
            //b.job = j;
            b.Start(j);
        }