C# - 如何并行运行5个任务 [英] C# - how to run 5 tasks in parallel

查看:392
本文介绍了C# - 如何并行运行5个任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我使用以下代码在并行中调用5个方法,然后等待接收来自所有方法的响应,然后再继续。



Hi All,

I am using the below code to Call 5 methods in Parallel and then waiting to received response from all the methods before proceeding further.

Task<string> t1 = Task<string>.Factory.StartNew(() => LookUp1(lookUpObj)); //09:28:41.594538
Task<string> t2 = Task<string>.Factory.StartNew(() => LookUp2(lookUpObj)); //09:28:41.593538
Task<string> t3 = Task<string>.Factory.StartNew(() => LookUp3(lookUpObj)); //09:28:41.593538
Task<string> t4 = Task<string>.Factory.StartNew(() => LookUp4(lookUpObj)); //09:28:41.594538
Task<string> t5 = Task<string>.Factory.StartNew(() => LookUp5(lookUpObj)); //09:28:42.621597
Task.WaitAll(t1, t2, t3, t4, t5);





I已经放入了一些日志语句来确认是否并行调用这些方法。前4个方法与大约100毫秒的时间差并行调用,但最后一个(第5个)方法在完成1秒后与其他方法相比调用。我在上面的评论中给出了调用时间示例。



请让我知道我做错了什么,或者对并行呼叫的数量有任何限制或建议任何方式实现这一点。在此先感谢。



谢谢,

Akash Kansal。



< b>我尝试了什么:





I have put in some log statements to confirm if these methods are getting called in parallel or not. First 4 methods are getting called in parallel with a time diff of around 100 milli seconds but the last one (5th one) method is invoked after a complete 1 second compared to others. I have given invoke time example in comments above.

Please let me know what I am doing wrong or is there any limitation on number of parallel calls or suggest any way to achieve this. Thanks in advance.

Thanks,
Akash Kansal.

What I have tried:

Parallel.Invoke(() => { r1 = Iteration1LookUp(lookUpObj); },
                () => { r2 = Iteration2LookUp(lookUpObj); },
                () => { r3 = Iteration3LookUp(lookUpObj); },
                () => { r4 = Iteration4LookUp1(lookUpObj); },
                () => { r5 = Iteration4LookUp2(lookUpObj); });



试过这个,但同样的结果。


Tried this, but same kind of result.

推荐答案

线程很复杂,因为它在确切的线程运行时是不确定的,以及它们运行的​​时间。

问题是每个线程都需要运行一个核心:因此处理器拥有的核心越多,它实际上可以同时运行的线程就越多。如果你有两个核心,那么只有两个线程可以同时运行,四个核心,四个线程等等。

但这不是完整的图片,因为它不仅仅是你想要的线程核心空间:Windows中充满了线程,其中许多都在任何特定时刻都很忙 - 启动任务管理器,您可以看到此时正在运行的进程数,并了解它们使用了多少CPU。



所以当你要求五个线程时,这并不意味着你将同时运行五个任务 - 如果你有足够的内核,你可能会现实世界你可能不会。并且你在那里发现越多的监控来找出正在运行的线程,你对正在运行的线程的影响越大并且扭曲图片!
Threading is complicated, because it's non-deterministic when exactly thread runs, and how long they run for.
The "problem" is that each thread requires a core to run on: so the more cores your processor has, the more thread it can actually run at the same time. If you have two cores, then only two threads can run at the same time, four cores, four threads, and so on.
But that's not the complete picture, because it's not just your threads that want space on a core: Windows in full of thread, many of which are busy at any particular moment - bring up Task Manager and you can see how many processes are running at this moment, and get an idea how much CPU they are using.

So when you ask for five threads, that doesn't mean that you will get five tasks running in parallel - you might, if you have enough cores, but in the real world you probably won't. And the more monitoring you put in there to find out what threads are running, the more you affect the threads that are running and distort the picture!


这篇关于C# - 如何并行运行5个任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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