|
using System.Collections;
using System.Threading;
namespace Sample.Tests
{
public class SimpleThreadPool
{
#region Constructors
public SimpleThreadPool(ArrayList commands, int threadCount)
{
this.commands = commands;
this.threadCount = threadCount;
}
#endregion
#region Methods
/// <summary>
///
/// </summary>
public void Execute()
{
Thread[] pool = new Thread[this.threadCount];
int commandIndex = 0;
while (true)
{
if (commandIndex == this.commands.Count)
{
break;
}
for (int i = 0; i < pool.Length; i++)
{
if (pool[i] != null && pool[i].IsAlive)
{
continue;
}
else
{
ICommand command = (ICommand)this.commands[commandIndex];
pool[i] = new Thread(new ThreadStart(command.Execute));
pool[i].Start();
commandIndex++;
break;
}
}
Thread.Sleep(50 / this.threadCount);
}
for (int i = 0; i < pool.Length; i++)
{
if (pool[i] != null)
{
pool[i].Join();
}
}
}
#endregion
#region Members
private ArrayList commands;
private int threadCount;
#endregion
}
}
|
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.
Independent contract developer for .NET data-oriented systems.
not much to say about myself but feel free to contact me for any inquiries and comments!