请帮助线程 [英] Please help threading

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

问题描述

我有一个用于ex的对象集合

I have a collection of objects for ex

List<Employee> objEmployeeList = new List<Employee>();

            for (int i = 1; i < 300; i++)
            {

                Employee objEmployee = new Employee { FName = "test" + i, Name = "test" + i };
                objEmployeeList.Add(objEmployee);
            }



我有300个对象的集合...

我想将其分成30个部分并发送给30个线程,

例如,我有一个方法



I have this collection of 300 objects...

i want to split this into 30 parts and send them to 30 Threads,

For ex i have a method

Processeemployee(Employee objEmployee)



我想为此方法创建30个线程,并且每个方法应并行处理30个对象.

请帮助我..



I want to create 30 threads for this method and each method should process 30 objects parallely.. Is this possible..


Please help me..

推荐答案

请参阅我对ASP.NET的评论.

现在,让我们分析可能并行执行循环的结果. Employee的每个实例的构造都是独立的,因此可以并行创建每个实例.但是我们可以合理地假设构造函数的执行速度非常快,几乎不会比向列表中添加新元素要慢(如果速度较慢,则意味着您在使用该构造函数时根本犯了错).

但是现在,请注意objEmployeeList是所有线程共享的资源.这意味着应该完成线程保存.就是说,您可以将objEmployeeList用于线程安全类型System.Collections.Concurrent.ConcurrentBag<Employee>或常规"列表类System.Collections.Generic.List<Employee>,但是Add操作应在lock语句下执行.

请参阅:
http://msdn.microsoft.com/en-us/library/dd381779.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/c5kehkcz%28v = vs.80%29.aspx [ ^ ].

是好是坏? 这实际上将使并行执行的效果无效,因为线程安全地将元素添加到同一集合中会通过将所有线程放入等待访问Add的队列中来序列化所有线程.每个线程将快速执行构造函数,并迅速在线程的同一单个队列中占有一席之地,每个队列均等待对Add的访问.

它的实际作用是什么? 这实际上会减慢该任务的执行速度,因为即使在许多CPU内核上,您也不会获得可观的并行化;同时,使用线程和同步的开销成本将占用CPU时间的很大一部分.

与其明确使用线程,不如最好使用System.Threading.Tasks.Parallel.ForEach http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.aspx [
Please see my comment about ASP.NET.

Now, let''s analyze the effect of possible parallel execution of your loop. Construction of each instance of Employee is independent, so each can be created in parallel. But we can reasonably assume that a constructor execution is very fast, would hardly be slower than adding a new element to the list (if it is slower, it would mean you are doing something fundamentally wrong with this constructor).

But now, pay attention that objEmployeeList is a resource shared by all your threads. It means that is should be done thread-save. That said, you could use for objEmployeeList either the thread-safe type System.Collections.Concurrent.ConcurrentBag<Employee> or a "regular" list class System.Collections.Generic.List<Employee>, but the Add operation should be performed under the lock statement.

Please see:
http://msdn.microsoft.com/en-us/library/dd381779.aspx[^],
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^],
http://msdn.microsoft.com/en-us/library/c5kehkcz%28v=vs.80%29.aspx[^].

Is is good or bad? It will practically nullify the effect of parallel execution, because thread-safe adding the elements to the same collection will serialize all threads by putting them in a queue waiting for access to Add. Each thread will quickly execute the constructor and quickly take its place in the same single queue of the threads each waiting for the access to Add.

What''s the practical effect of it? It will actually slow down the execution of this task, because you won''t gain considerable parallelization even on many CPU cores; at the same time, the overhead cost of using threads and synchronization will eat up a good part of CPU time.

Instead of explicit use of thread you could better use System.Threading.Tasks.Parallel.ForEach, http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.aspx[^].

But will it really be much better in this case? No! The exact same considerations will be applicable, with the same result: parallel execution is useless in this case.

The code using parallel execution for performance gain is neither usual not trivial. This approach is not applicable to everything; and the set of reasonable tasks is limited, the application of parallel execution should start with thorough analysis and design. In many cases, parallel execution as such does not help to gain sheer performance.

东方谚语说:

仅仅说"halva-halva"不会使你的嘴变甜."

"Just saying ''halva-halva'' won''t make your mouth sweet."



—SA



—SA


答案是肯定的.

去找背景处理工人.它为您提供线程安全.

Answer is yes.

Go for the back ground process worker. It''s provide you thread safety.


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

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