是否有可能在同一时间运行多个线程500种不同的操作? [英] Is it possible to run 500 different operations in multiple threads at the same time?

查看:105
本文介绍了是否有可能在同一时间运行多个线程500种不同的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,比方说,有500个对象的列表。对于每一个对象,我调用一个函数计算它的成本。因此,每个的500呼叫独立于其他人。总体大约需要30秒。那岂不是可以运行所有的500任务的同时,他们不依赖对方?我什么都不知道多线程,因此我不知道,如果它可能是一个解决方案。

I have, let's say, a list with 500 objects. For each object, I'm calling a function calculating it's cost. So each of the 500 calls is independent from the others. The overall takes around 30 seconds. Wouldn't it be possible to run all the 500 tasks at the same time as they don't rely on each other ? I know nothing about multi-threading therefore I don't know if it could be a solution.

推荐答案

您可以轻松地使用的 Parallel.ForEach方法

Parallel.ForEach(items, item =>
{
    item.CalculateCost();
});

这篇关于是否有可能在同一时间运行多个线程500种不同的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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