Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C# 4.0

Extended Thread Pool

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
6 Apr 2013Ms-PL3 min read 81.8K   1.8K   119  
Your own extensible and configurable Thread Pool.
using System;
using System.Collections.Generic;
using System.Threading;
using Nelibur.Sword.DataStructures.Queues;
using Nelibur.Sword.Extensions;
using Nelibur.Sword.Logging;

namespace Nelibur.Sword.Threading.Processors
{
    public abstract class TaskProcessor<TTask> : IDisposable
    {
        private readonly ILog _log;
        private readonly BlockingQueue<TTask> _queue = new BlockingQueue<TTask>();
        private volatile bool _isRun = true;
        private Thread _workThread;

        protected TaskProcessor()
        {
            _log = LogManager.GetLogger(GetType());
            Initialise();
        }

        public int Count
        {
            get { return _queue.Count; }
        }

        /// <summary>
        ///     Processes the task.
        /// </summary>
        /// <param name="task">The task.</param>
        public void Enqueue(TTask task)
        {
            _queue.Enqueue(task);
        }

        /// <summary>
        ///     Processes tasks.
        /// </summary>
        /// <param name="tasks">Tasks.</param>
        public void Enqueue(IEnumerable<TTask> tasks)
        {
            _queue.Enqueue(tasks);
        }

        public void Dispose()
        {
            if (_isRun == false)
            {
                return;
            }
            _isRun = false;
            DisposeCore();
        }

        protected virtual void DisposeCore()
        {
        }

        protected abstract void ProcessTaskCore(TTask task);

        private void Initialise()
        {
            _workThread = new Thread(ProcessTask)
            {
                Name = GetType().Name,
                IsBackground = true
            };
            _workThread.Start();
        }

        private void ProcessTask()
        {
            while (_isRun)
            {
                try
                {
                    TTask task = _queue.Dequeue();
                    if (task.IsNull())
                    {
                        continue;
                    }
                    _log.DebugFormat("Processing task: {0}", task);
                    ProcessTaskCore(task);
                }
                catch (Exception ex)
                {
                    _log.Error(ex);
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
United States United States
B.Sc. in Computer Science.

Comments and Discussions