C# ThreadPool 一个线程阻塞另一个线程?

发布于 2024-11-14 15:57:08 字数 1161 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情域 2024-11-21 15:57:08

首先,您应该将 responseresStream 包装在 using 块中,以确保它们得到正确处理。

其次,在我看来,将长时间运行的线程放入ThreadPool中并不是最好的主意。 ThreadPool 类的想法是,它允许您对相对较小的操作进行排队,而为每个操作生成一个新线程将是一种浪费。如果您的操作需要花费大量时间来运行,我建议实际上为每个操作创建专用的 Thread 对象。

Firstly, you should wrap both response and resStream in using 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 the ThreadPool 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 dedicated Thread objects for each.

手心的温暖 2024-11-21 15:57:08

您确实应该中断调试器以找出每个线程停止的位置,但我的猜测是罪魁祸首是

mySqlManager.SaveResult(tempString);

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.SaveResult(tempString);

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文