C# 线程池的代码帮助
已经创建了一个实现ThreadPool的类。代码如下: 公共密封类PyeThreadPool:C# 线程池的代码帮助,c#,multithreading,C#,Multithreading,已经创建了一个实现ThreadPool的类。代码如下: 公共密封类PyeThreadPool: IDisposable { private readonly object _lock = new object(); private readonly int _minThreadCount; private readonly int _maxThreadCount; private readonly Queue<Action> _queue = ne
IDisposable
{
private readonly object _lock = new object();
private readonly int _minThreadCount;
private readonly int _maxThreadCount;
private readonly Queue<Action> _queue = new Queue<Action>();
private int _totalThreadCount;
private int _waitingThreadCount;
private bool _disposed;
public PyeThreadPool(int minThreadCount, int maxThreadCount)
{
if (minThreadCount < 0)
throw new ArgumentOutOfRangeException("minThreadCount");
if (maxThreadCount < 1 || maxThreadCount < minThreadCount)
throw new ArgumentOutOfRangeException("maxThreadCount");
_minThreadCount = minThreadCount;
_maxThreadCount = maxThreadCount;
}
public void Dispose()
{
lock (_lock)
{
_disposed = true;
// if there are thread waiting, they should stop waiting.
if (_waitingThreadCount > 0)
Monitor.PulseAll(_lock);
}
}
/// <summary>
/// Executes an action in a parallel thread.
/// </summary>
public void RunParallel(Action action)
{
if (action == null)
throw new ArgumentNullException("action");
lock (_lock)
{
if (_disposed)
throw new ObjectDisposedException(GetType().FullName);
bool queued = false;
if (_waitingThreadCount == 0)
{
if (_totalThreadCount < _maxThreadCount)
{
_totalThreadCount++;
var thread = new Thread(_ThreadRun);
thread.Name = "Worker Thread";
thread.Start(action);
queued = true;
}
}
if (!queued)
{
_queue.Enqueue(action);
Monitor.Pulse(_lock);
}
}
}
private void _ThreadRun(object firstAction)
{
Action action = (Action)firstAction;
firstAction = null;
// we always start a new thread with an action, so we get it immediately.
// but, as we don't know what that action really holds in memory, we set
// the initial action to null, so after it finishes and a new action is get,
// we will let the GC collect it.
while (true)
{
action();
lock (_lock)
{
if (_queue.Count == 0)
{
// we started waiting, so new threads don't need to be created.
_waitingThreadCount++;
while (_queue.Count == 0)
{
if (_disposed)
return;
if (_totalThreadCount > _minThreadCount)
{
_totalThreadCount--;
_waitingThreadCount--;
return;
}
action = null;
Monitor.Wait(_lock);
}
// we finished waiting.
_waitingThreadCount--;
}
action = _queue.Dequeue();
// we just get a new action, and we will release the lock and return
// to the while, where the action will be executed.
}
}
}
}
问题是:
(1) 当我在调试或发布模式下执行此操作时,我在文本框中看不到结果,另一方面,我在逐步执行时看到结果。我在这里遗漏了什么,为什么我看不到输出
(2) RunParallel方法仅显示一个线程,即使我已将maxcount设置为大于1。是否缺少任何代码逻辑,或者是因为测试应用程序很简单
谢谢 你应该看看图书馆。它是ThreadPool的最佳替代方案之一
其功能(从源链接复制)
智能线程池是用C#编写的线程池。该实现最初基于Stephan Toub的线程池,并带有一些额外的功能,但现在,它远远超出了最初的功能。以下是线程池功能的列表:
- 线程数根据池中线程的工作负载动态变化。 工作项可以返回一个值
- 如果尚未执行工作项,则可以取消该工作项
- 在执行工作项时使用调用线程的上下文(受限)
- 使用最少数量的Win32事件句柄,因此应用程序的句柄计数不会爆炸
- 调用方可以等待多个或所有工作项完成
- 工作项可以具有PostExecute回调,该回调在工作项完成后立即调用
- 工作项附带的状态对象可以自动释放
- 将工作项异常发送回调用者
- 工作项具有优先权
- 工作项目组
- 调用方可以挂起线程池和工作项组的开始
- 线程具有优先级
- 线程具有初始化和终止事件
- WinCE平台受支持(有限)
- 支持Action和Func泛型方法
- 支持Silverlight
- 支持单声道
- 性能计数器(Windows和内部)
- 工作项超时(被动)
- 线程公寓
- 螺纹是圆的
- 线程名称模板
- 支持Windows Phone(有限)
- 线程最大堆栈大小
您需要在ShowMessage函数中执行BeginInvoke或Invoke操作。我想ShowMessage是一种运行在WinForm或WPF上并需要其功能性的功能?如果是这样的话,这些技术不支持多线程(虽然我不是100%确定WPF),我明白了,但因为这是web应用程序而不是桌面,所以必须重新考虑测试应用程序的方法。啊,你可以将字符串附加到线程安全的集合中,等待所有线程完成,然后输出结果。
PyeThreadPool MyPool;
int x = 1;
protected void Page_Load(object sender, EventArgs e)
{
MyPool = new PyeThreadPool(4, 6);
}
void showMessage(string message)
{
TxtMessage.Text = message;
}
protected void BtnStartThread_Click(object sender, EventArgs e)
{
x++;
int arg = x;
MyPool.RunParallel(() =>
{
showMessage(arg.ToString());
});
}