Exemplo n.º 1
0
        /// <summary>
        /// Creates FixedThreadPoolService
        /// Throws ArgumentOutOfRangeException if nThreads <code><= 0</code>
        /// Очередь может держать не более int.MaxValue задач.
        /// </summary>
        /// <param name="nThreads">Number of threads should be in range [1; MAX_NUMBER_OF_THREADS]</param>
        /// <param name="poolManager">Manages FixedThreadPool</param>
        public FixedThreadPoolService(
            IPriorityTaskQueueStrategy strategy,
            int nThreads)
        {
            if (nThreads <= 0)
            {
                throw new ArgumentOutOfRangeException("nThreads",
                                                      "nThreads must be in range [1; MAX_NUMBER_OF_THREADS]");
            }

            this.maxThreads = nThreads;

            this.handlers    = new WaitHandle[2];
            this.mutex       = new Mutex(false);
            this.handlers[0] = this.mutex;
            this.semaphore   = new Semaphore(0, int.MaxValue);
            this.handlers[1] = this.semaphore;

            this.taskQueue = new PriorityTaskQueue(strategy);
            this.pool      = new Thread[nThreads];

            for (int i = 0; i < nThreads; i++)
            {
                Thread t = new Thread(this.RunTask);
                t.Name = "thread" + i;
                Log.Info("starting thread {0}", t.Name);
                this.pool[i] = t;
                t.Start();
            }
        }
        public PriorityTaskQueue(IPriorityTaskQueueStrategy strategy)
        {
            this.strategy = strategy;

            this.tasks = new Dictionary <Priority, Queue <WCTask> >();
            this.tasks.Add(Priority.HIGH, new Queue <WCTask>(new Queue <WCTask>()));
            this.tasks.Add(Priority.NORMAL, new Queue <WCTask>(new Queue <WCTask>()));
            this.tasks.Add(Priority.LOW, new Queue <WCTask>(new Queue <WCTask>()));
        }