我自己创建了一个线程池,是Stack<Thread>结构的,
class CopyMeterThreadPool
{
Stack<Thread> _pool; public CopyMeterThreadPool(int capability)
{
_pool = new Stack<Thread>(capability);
} public void Push(Thread thread)
{
if (thread == null)
throw new ArgumentNullException("线程池的线程未初始化!");
lock (_pool)
{
_pool.Push(thread);
}
} public Thread Pop()
{
lock (_pool)
{
return _pool.Pop();
}
} public int Count
{
get { return _pool.Count; }
} }
调用就是通过Pop()取出线程工作。
我现在碰到的问题是怎么重用Stack中取出的线程,怎么判断Pop()出来的线程结束了,然后再Push(),高手给个思路。谢了。
class CopyMeterThreadPool
{
Stack<Thread> _pool; public CopyMeterThreadPool(int capability)
{
_pool = new Stack<Thread>(capability);
} public void Push(Thread thread)
{
if (thread == null)
throw new ArgumentNullException("线程池的线程未初始化!");
lock (_pool)
{
_pool.Push(thread);
}
} public Thread Pop()
{
lock (_pool)
{
return _pool.Pop();
}
} public int Count
{
get { return _pool.Count; }
} }
调用就是通过Pop()取出线程工作。
我现在碰到的问题是怎么重用Stack中取出的线程,怎么判断Pop()出来的线程结束了,然后再Push(),高手给个思路。谢了。
抱歉,我对泛型还不是很清楚。。看过,但是忘记了。
如果是你自己写的类,那么你可以写个线程结束的事件,如果这个stack类中的一个线程结束了,那么就触发这个事件,在这个事件中,你可以调用你的push。如果是系统的Stack类,那么你就可以继承Stack类,自己写一个Stack类,在你自己的类里,你添加这个线程结束的事件就好了
using System.Collections.Generic;using System.Text;
using System.Threading;namespace Demo
{
enum ThreadStatus { Running,Stop}; class CopyMeterThreadPool
{
Stack<Thread> _pool; private int _num; //static delegate void ThreadStop(object sender, EventArgs e); //static event ThreadStop OnThreadStop; public CopyMeterThreadPool(int capability)
{
_num = capability;
_pool = new Stack<Thread>(capability);
} public void Push(Thread thread)
{
if (thread == null)
throw new ArgumentNullException("线程池的线程未初始化!");
lock (_pool)
{
SimpleThread st = new SimpleThread(thread);
st.OnThreadStop += new SimpleThread.ThreadStop(st_OnThreadStop);
//OnThreadStop+=new ThreadStop(CopyMeterThreadPool_OnThreadStop);
_pool.Push(thread);
}
} void st_OnThreadStop(object sender, ThreadEventArgs e)
{
if (e.Status == ThreadStatus.Stop)
{
Thread t = (Thread)sender;
Push(t);
Console.WriteLine("成功放入线程池!");
}
} public Thread Pop()
{
lock (_pool)
{
return _pool.Pop();
}
} //public void Init(object obj)
//{
// for (int i = 0; i < _num; i++)
// {
// Thread th = new Thread(new ParameterizedThreadStart(obj));
// }
//} public int Count
{
get { return _pool.Count; }
} public void SetStop(Thread t)
{
if (t.ThreadState == ThreadState.Running)
{
t.Abort();
}
} protected static void CopyMeterThreadPool_OnThreadStop(object sender, ThreadEventArgs e)
{
}
}
class SimpleThread
{
private Thread thread; public delegate void ThreadStop(object sender, ThreadEventArgs e); public event ThreadStop OnThreadStop;
public SimpleThread(Thread t)
{
thread = t;
if (OnThreadStop != null)
{
if (thread.ThreadState == ThreadState.Stopped)
{
OnThreadStop(thread, new ThreadEventArgs(ThreadStatus.Stop));
}
}
}
} class ThreadEventArgs:EventArgs
{
private ThreadStatus status; internal ThreadStatus Status
{
get { return status; }
} public ThreadEventArgs(ThreadStatus status)
: base()
{
this.status = status;
}
} class PoolManager
{
private Stack<Thread> _stack;
public PoolManager(Stack<Thread> stack)
{
this._stack = stack;
}
}
}
using System;
using System.Collections.Generic;using System.Text;
using System.Threading;namespace Demo
{
enum ThreadStatus { Running,Stop}; class CopyMeterThreadPool
{
Stack<Thread> _pool; private int _num; //static delegate void ThreadStop(object sender, EventArgs e); //static event ThreadStop OnThreadStop; public CopyMeterThreadPool(int capability)
{
_num = capability;
_pool = new Stack<Thread>(capability);
} public void Push(Thread thread)
{
if (thread == null)
throw new ArgumentNullException("线程池的线程未初始化!");
lock (_pool)
{
SimpleThread st = new SimpleThread(thread);
//注册事件
st.OnThreadStop += new SimpleThread.ThreadStop(st_OnThreadStop);
//OnThreadStop+=new ThreadStop(CopyMeterThreadPool_OnThreadStop);
_pool.Push(thread);
}
} //事件委托的回收线程方法
void st_OnThreadStop(object sender, ThreadEventArgs e)
{
if (e.Status == ThreadStatus.Stop)
{
Thread t = (Thread)sender;
Push(t);
Console.WriteLine("成功放入线程池!");
}
} public Thread Pop()
{
lock (_pool)
{
return _pool.Pop();
}
} //public void Init(object obj)
//{
// for (int i = 0; i < _num; i++)
// {
// Thread th = new Thread(new ParameterizedThreadStart(obj));
// }
//} public int Count
{
get { return _pool.Count; }
} public void SetStop(Thread t)
{
if (t.ThreadState == ThreadState.Running)
{
t.Abort();
}
} protected static void CopyMeterThreadPool_OnThreadStop(object sender, ThreadEventArgs e)
{
}
} //封装线程的类
class SimpleThread
{
private Thread thread;
//声明委托和事件
public delegate void ThreadStop(object sender, ThreadEventArgs e); public event ThreadStop OnThreadStop;
public SimpleThread(Thread t)
{
thread = t;
if (OnThreadStop != null)
{
if (thread.ThreadState == ThreadState.Stopped)
{
OnThreadStop(thread, new ThreadEventArgs(ThreadStatus.Stop));
}
}
}
}
//参数类
class ThreadEventArgs:EventArgs
{
private ThreadStatus status; internal ThreadStatus Status
{
get { return status; }
} public ThreadEventArgs(ThreadStatus status)
: base()
{
this.status = status;
}
}
//这个类没有用
class PoolManager
{
private Stack<Thread> _stack;
public PoolManager(Stack<Thread> stack)
{
this._stack = stack;
}
}
}