使用ThreadPool实现多线程的问题 [英] Problem in implementing multithreading using ThreadPool

查看:52
本文介绍了使用ThreadPool实现多线程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在实现多线程.
我正在逐行读取文本文件中的数据,然后使用线程进一步处理这些行.
下面是我的示例代码:

Hi All,

I am implementing multithreading.
I am reading data from a text file line by line and then using threads to process these lines further.
Below is my sample code:

public void HandleRequest(TextReader reader)
{
            string line = "";
            while ((line = reader.ReadLine()) != null)
            {
                string[] arrUserInfo = line.Split(new char[] { ',' });
                try
                {
                    Thread objThread = new Thread(new ParameterizedThreadStart(this.Add));
                    objThread.Start((object)arrUserInfo);
                }
                catch (Exception ex)
                {
                }
            }
       }

public void Add(object arrUserInfo)
{
    //Do something
}



在运行了1000行应用程序后,我发现如果不使用线程,其性能也不会有太大差别.
我想提高应用程序的性能.
我在Google上阅读到ThreadPool可能会有所帮助.
我提到了 http://www.switchonthecode.com/tutorials/csharp-tutorial-using-the-线程池的[ ^ ]文章. br/> 但是我无法理解如何在我的应用程序中实现ThreadPool,因为ThreadPool使用For循环,而我的应用程序已经使用while循环从文本文件中读取行.

任何人都可以在这里帮助我.

谢谢,
Nagendra.



After running application for 1000 lines, i found out that its performance is not much different if we don''t use threads.
I want to improve the performance of my application.
I read on google that ThreadPool can be helpful.
I referred http://www.switchonthecode.com/tutorials/csharp-tutorial-using-the-threadpool[^] article for ThreadPool.
But i am not able to understand how can i implement ThreadPool in my application because ThreadPool uses a For loop and my application already using a while loop to read lines from text file.

Can anyone help me here.

Thanks,
Nagendra.

推荐答案

线程可能根本无法提高应用程序的性能:在某些情况下甚至可能使性能更糟.特别是当您创建大量线程来分别执行小的(几乎是琐碎的)任务时,设置线程所涉及的开销可能会严重超过您获得的收益.请记住,为了获得性能提升,您需要每个线程都在其自己的处理器内核上运行,并且操作系统本身,其他应用程序对内核的可用性还有其他要求,然后开始看到问题.如果要提高性能,则需要查看现有代码中的每个线程正在尝试做什么,并检查瓶颈可能在哪里.可能是通过一次读取所有行,并使用两个线程分别处理一半的记录来改进您的代码-我不知道.或者可能是您需要查看如何添加"这些行.

仅生成大量线程不太可能真正带来收益.
Threading may not improve the performance of your app at all: it may even make it worse in some cases. Particularly when you are creating a large number of threads to do small (almost trivial) tasks each, the overhead involved in setting up the thread can seriously outweigh the advantages your gain. Bear in mind that in order to get performance gains, you need each thread operating on it''s own processor core and that there are other demands on the availability of cores from the OS itself, other applications, and so forth, and you start to see the problem. If you want to improve performance, then you need to look at what each thread in your existing code is trying to do, and examine where you bottlenecks are likely to be. It may be that your code could be improved by just reading all the lines at a time, and using two threads to each process half the records - I don''t know. Or it could be that you need to look at how you are "adding" these lines.

Just generating massive numbers of threads is unlikely to give real gains.


这篇关于使用ThreadPool实现多线程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆