Click here to Skip to main content
15,885,655 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.7K   1.8K   119  
Your own extensible and configurable Thread Pool.
using System;
using System.Configuration;
using System.Threading;
using Zulu.Threading.ThreadPools.TaskItems;
using Zulu.Threading.ThreadPools.TaskQueueControllers;
using log4net;

namespace Zulu.Threading.ThreadPools
{
    internal sealed class WorkThread
    {
        private readonly ILog _log = LogManager.GetLogger(typeof(WorkThread));
        private readonly ITaskQueueController _taskQueueController;
        private readonly Thread _thread;
        private volatile bool _isRun = true;

        private WorkThread(Builder builder)
        {
            _taskQueueController = builder.TaskQueueController;
            _thread = new Thread(DoWork)
                {
                    Name = builder.Name,
                    IsBackground = true,
                };
        }

        public void Start()
        {
            _thread.Start();
        }

        public void Stop()
        {
            _isRun = false;
            _taskQueueController.Dispose();
            _thread.Join();
        }

        private static void ProcessItem(ITaskItem item)
        {
            item.DoWork();
        }

        private void DoWork()
        {
            while (_isRun)
            {
                try
                {
                    IWorkItem workItem = _taskQueueController.Dequeue();
                    if (workItem == null)
                    {
                        continue;
                    }
                    ProcessItem(workItem);
                }
                catch (Exception ex)
                {
                    _log.Error(ex);
                }
            }
        }

        internal sealed class Builder
        {
            public string Name { get; set; }
            public ITaskQueueController TaskQueueController { get; set; }

            public WorkThread Build()
            {
                Validate();
                return new WorkThread(this);
            }

            private void Validate()
            {
                if (TaskQueueController == null)
                {
                    throw new NullReferenceException();
                }
                if (string.IsNullOrWhiteSpace(Name))
                {
                    throw new ConfigurationErrorsException("WorkThread Name is null or whitespace");
                }
            }
        }
    }
}

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