开发者

How to get multi-thread test?

I just started C# programming study. These days I'm studying performance test for specified calculation in .NET 4.0. The system read and copy a number of data, and is calcultaed by my own function.

I'll try to do that for over 300 data set using multi thread system. All threads are started from receiving data, so I'll proceed this part with EventHandler.

I'm confused with how to construct threads. Surely, I can make those as List and work the function one by one like:

Function la = new Function();
List<Thread> threadSet = new List<Thread>();
for (int i = 0; i < 300; 开发者_如何学Goi++)
  threadSet.Add(new Thread(new ThreadStart(la.doWork)));
for (int i = 0; i < 300; i++)
  threadSet[i].Start();

Or is there any better way of? I saw Thread Pool issue, but I don't know how differ from that.

And I wonder if event includes constructing thread is better of thread includes event handler.

English is not my mother language, so maybe I was so wrong for expression about my question.

Thank you. Please, teach me!


Having so many threads not always increase the performance. It depends on your task (is it I/O bound or CPU bound) and how many tasks you have.

In .Net 4.0 you have PLinq library. It will try to run optimally for your hardware so it will choose how much parralism need to have for your environment it self.Read this article. http://msdn.microsoft.com/en-us/magazine/cc163329.aspx


with .Net 4.0 the simplest way to do exactly the same thing would be :

Parallel.For(0,300, _ => la.doWork() );
// or with plinq
Enumerable.Range(1,300).AsParallel().ForAll(_ => la.doWork() );

The advantage with these solution is you won't have to deal with the thread creation and management and clutter your code with it. I should note that this will not necessarily create a thread for every work item.

Now this code is optimal only if the la.dowork() is a CPU only task, If it involves any I/O (reading from a file or the network ) it could be very inefficient because a good portion of the time spent by the thread will be wasted on waiting for data.

Could you give us a little more detail about what you are doing (where do you read the data and what do you do with it) ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜