需要有关如何将其编写为多线程应用程序的指导
我正在开发一个应用程序,它目前的样子如下。 我想知道我是否可以获得一些帮助,以提高效率。 也希望利用任务并行库(如果它有帮助的话)。 现在的设计方式没有任何限制,这意味着我可以完全重新设计应用程序的任何部分。
class Program
{
static void Main(string[] args)
{
IList<ISystem> systems = GetSystems();
if (systems.Where(s => s.Import = true).Count() == 0)
return;
var export = new Export();
// Import & Export People
exportData.LoadPeople();
foreach(var system in systems)
foreach(var person in export.People)
system.Push(person);
export.LoadLocations();
foreach(var system in systems)
foreach(var location in export.Locations)
system.Push(location);
export.LoadOtherData();
foreach(var system in systems)
system = system as IDifferentSystem;
if (system == null) continue;
foreach(var data in export.OtherData)
system.Push(data);
}
}
感谢您的帮助
I have an app that i'm working on and here's what it currently looks like.
I was wondering if i could get some help making this a bit more efficient.
Was looking to make use of Task Parallel Library as well (that is if it would help).
There are no restrictions on how things are designed right now, meaning I can completely re-design any part of the app.
class Program
{
static void Main(string[] args)
{
IList<ISystem> systems = GetSystems();
if (systems.Where(s => s.Import = true).Count() == 0)
return;
var export = new Export();
// Import & Export People
exportData.LoadPeople();
foreach(var system in systems)
foreach(var person in export.People)
system.Push(person);
export.LoadLocations();
foreach(var system in systems)
foreach(var location in export.Locations)
system.Push(location);
export.LoadOtherData();
foreach(var system in systems)
system = system as IDifferentSystem;
if (system == null) continue;
foreach(var data in export.OtherData)
system.Push(data);
}
}
Thanks for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这完全取决于您所说的“高效”是什么意思,以及您想要解决的问题与维护多线程设计的成本。
对于一些非常一般的建议,对于三类问题,多线程设计是合适的:
上面的内容有点过于简单化,但要点是,确保您正在解决正确的问题,而不是强迫您的问题成为预先设想的解决方案。决定使用多线程设计时需要考虑很多因素。自动化测试将变得更加困难。您的设计是否可以通过执行的正确性(无崩溃/挂起/数据损坏)以及您对特定应用程序的性能定义进行维护?当然,TPL 使多线程设计变得更容易,但它绝对不能完全消除这些成本。
始终考虑替代方案。例如,上面的 #1 也可以使用异步的方式来完成,也许 I/O 回调稍后会在您的调用线程上运行(即使这是内部多线程的,但您不需要关心这一点)。这仍然是单线程设计,不用担心锁。同样对于#2,可以使用更有效的算法和/或数据结构布局来实现性能。
It all depends on what you mean by "efficient", and what problems you are trying to solve vs. the cost of maintaining a multi-threaded design.
For some extremely general advice, there are three types of problems where a multi-threaded design is appropriate:
The above is a bit over-simplified, but the point is, make sure you are solving the right problem as opposed to forcing your problem into a preconceived solution. Lots of factors to consider when deciding to use a multi-threaded design. Automated testing will become more difficult. Will your design be maintainable with correctness of execution (no crash/hang/data corruption) and with your definition of performance for a particular app? Sure the TPL makes a multi-thread design easier, but it definitely doesn't remove these costs altogether.
Always consider the alternatives. For example #1 above can also be done using something asynchronous, perhaps an I/O callback gets run on your calling thread later on (even though this is internally multi-threaded, but you don't need to care about that). This is still a single-threaded design, no locks to worry about. Also for #2, it may be possible to use a more efficient algorithm and/or data structure layout to achieve performance.