C# ThreadPool 一个线程阻塞另一个线程?
我有一个 C# 控制台应用程序,其中有一个线程池。在线程池中会有一个类执行一个连续的方法(直到它运行了一段时间或者知道何时停止)。该方法连接到 HttpWebResponse 流并不断重复它。
问题是,在短时间内,所有线程都只做自己的事情,但随后只有一个线程继续显示其输出,其余线程正在等待该线程。
这是为每个线程执行的方法。
function void ReadStream()
byte[] buf = new byte[8192];
String domain = streamingUrl
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(domain);
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
Stream resStream = response.GetResponseStream();
string tempString = null;
int count;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
tempString = Encoding.ASCII.GetString(buf, 0, count);
try
{
mySqlManager.SaveResult(tempString);
Console.WriteLine("Been here");
Thread.Sleep(1000);
}
catch (Exception e)
{
}
}
}
while (count > 0);
}
I have a C# console app where I have a threadpool. In the threadpool there will be a class thats executes a continuous method (till it has ran for a certain period of time or when it knows when to stop). The method connects to an HttpWebResponse stream and keep reeding it.
The problem is that for a short time all threads just do their things but then only one threads keep showing it's output and the rest is waiting for that thread.
This is the method that is executed for each thread.
function void ReadStream()
byte[] buf = new byte[8192];
String domain = streamingUrl
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(domain);
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
Stream resStream = response.GetResponseStream();
string tempString = null;
int count;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
tempString = Encoding.ASCII.GetString(buf, 0, count);
try
{
mySqlManager.SaveResult(tempString);
Console.WriteLine("Been here");
Thread.Sleep(1000);
}
catch (Exception e)
{
}
}
}
while (count > 0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您应该将
response
和resStream
包装在using
块中,以确保它们得到正确处理。其次,在我看来,将长时间运行的线程放入
ThreadPool
中并不是最好的主意。 ThreadPool 类的想法是,它允许您对相对较小的操作进行排队,而为每个操作生成一个新线程将是一种浪费。如果您的操作需要花费大量时间来运行,我建议实际上为每个操作创建专用的 Thread 对象。Firstly, you should wrap both
response
andresStream
inusing
blocks to ensure they are disposed of properly.Second, putting long-running threads in the
ThreadPool
is not really the best idea, in my opinion. The idea of theThreadPool
class is that it allows you to queue up relatively small actions where spawning a new thread for each one would be wasteful. If you have operations that will take a significant amount of time to run, I would advise actually creating dedicatedThread
objects for each.您确实应该中断调试器以找出每个线程停止的位置,但我的猜测是罪魁祸首是
mySqlManager 似乎是一个类属性,这意味着您需要特别小心线程安全。确保 SaveResult() 是线程安全的;如果不是,那么您需要通过锁定来使对 SaveResult() 的调用成为线程安全的。
祝你好运!
You should really break in the debugger to find out where each thread is stopped at, but my guess is that the culprit is
mySqlManager appears to be a class property, which means you need to be especially careful about thread safety. Make sure that SaveResult() is thread-safe; if not, then you need to make your call to SaveResult() thread-safe with locking.
Good luck!