编写自定义ThreadPool

时间:2017-05-26 09:59:12

标签: c# multithreading

我正在尝试编写一个自定义线程池对象来运行可能需要很长时间的某个函数。

我已将以下代码写入代码以运行模拟:

/var/www/html/wp$ sudo chmod 777 -R /var/www/html
/var/www/html/wp$ git init
Initialized empty Git repository in /var/www/html/wp/.git/

我得到以下结果: enter image description here

问题是:

  1. 模拟进程延迟namespace TPConsoleTest { class Program { private static MyThreadPool tp = null; static void Main(string[] args) { // Create new thread pool tp = new MyThreadPool(5); // Register on completion event tp.OnThreadDone += tp_OnThreadDone; for (int i = 0; i < 20; i++) { tp.AddThread(i); } // Wait for exit Console.ReadKey(); } static void tp_OnThreadDone(string name, int ix) { // Remove finished thread from pool tp.RemoveThread(name); // Write messsage Console.WriteLine("Thread {0} ended for : #{1}", name, ix); } } } namespace TPConsoleTest { class MyThreadPool { private IList<Thread> _threads; private int _maxThreads = 0; public bool IsPoolAvailable { get { if (this._threads != null && this._threads.Count(x => x.IsAlive) < this._maxThreads) return true; else return false; } } public delegate void ThreadDone(string name, int id); public event ThreadDone OnThreadDone; public MyThreadPool(int maxThreadCount) { // Set maximum number of threads this._maxThreads = maxThreadCount; // Initialize pool this._threads = new List<Thread>(); } public void AddThread(int ix) { // If maximum number is reached, wait for an empty slot while (!this.IsPoolAvailable) { Thread.Sleep(100); } // Create a unique name for the threae string tName = Guid.NewGuid().ToString(); Thread t = new Thread(() => { // Do this time-consuming operation MyWorker w = new MyWorker(); w.DoWork(tName, ix); }); t.IsBackground = true; t.Name = tName; t.Start(); // Add to pool this._threads.Add(t); // Fire event when done if (OnThreadDone != null) OnThreadDone(t.Name, ix); } public void RemoveThread(string name) { // Find event from name and remove it from the pool to allocate empty slot Thread t = this._threads.FirstOrDefault(x => x.Name == name); if (t != null) this._threads.Remove(t); } } } namespace TPConsoleTest { public class MyWorker { public void DoWork(string threadName, int ix) { // Dispatch a message signalling process start Console.WriteLine("Thread {0} started for : #{1}", threadName, ix); Random rnd = new Random(); int delay = rnd.Next(5); delay++; // Wait up to 5 second for each process. It may shorter or longer depending to a random value Thread.Sleep(delay * 1000); } } } 的命令似乎不起作用。一切都以闪电般的速度发生。
  2. 从消息中可以看出,在线程开始之前会收到一些线程结果。
  3. 如何修复这些错误?

1 个答案:

答案 0 :(得分:2)

您在创建线程后立即调用OnThreadDone,而不是等待它完成:

t.Start();    
this._threads.Add(t);

// The event is fired without waiting for the thread to end
if (OnThreadDone != null)
   OnThreadDone(t.Name, ix);

你应该在线程上的worker方法结束时调用它,而不是在启动线程

后立即调用它