需要有关如何将其编写为多线程应用程序的指导

发布于 2024-10-09 07:00:06 字数 1004 浏览 0 评论 0原文

我正在开发一个应用程序,它目前的样子如下。 我想知道我是否可以获得一些帮助,以提高效率。 也希望利用任务并行库(如果它有帮助的话)。 现在的设计方式没有任何限制,这意味着我可以完全重新设计应用程序的任何部分。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随梦而飞# 2024-10-16 07:00:06

这完全取决于您所说的“高效”是什么意思,以及您想要解决的问题与维护多线程设计的成本。

对于一些非常一般的建议,对于三类问题,多线程设计是合适的:

  1. 您不希望长时间运行的任务阻塞调用线程。这可能更适用于您不想阻塞主 GUI 线程的 GUI 应用程序,因此应用程序可以保持响应。
  2. 该算法就是所谓的“尴尬并行”,这意味着你可以细分数据并跨多个核心并行运行算法。
  3. 与本地或远程的其他进程通信。其他进程当然是其他线程。

上面的内容有点过于简单化,但要点是,确保您正在解决正确的问题,而不是强迫您的问题成为预先设想的解决方案。决定使用多线程设计时需要考虑很多因素。自动化测试将变得更加困难。您的设计是否可以通过执行的正确性(无崩溃/挂起/数据损坏)以及您对特定应用程序的性能定义进行维护?当然,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:

  1. You don't want a long-running task to block the calling thread. This is perhaps more applicable to GUI applications where you don't want to block the main GUI thread, so the app can stay responsive.
  2. The algorithm is the so-called "embarrassingly parallel", which means you can subdivide the data and run the algorithm in parallel across multiple cores.
  3. Communication with other processes, local or remote. Other processes are of course other threads.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文