关于多线程下载的缺点 [英] About multithreading download disadvantages

查看:319
本文介绍了关于多线程下载的缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于多线程下载的问题,正如您所知,使用多个线程进行下载可以提高应用程序的性能,但是有一些值得尊重的措施:例如线程数,可用带宽等等,但是我并不是真的了解,为什么应用程序的性能可能会因例如使用多个线程而降低,或者服务器的带宽,质量如何影响多线程应用程序的性能? ,在什么情况下单线程下载比多线程快?
感谢您的答复.

I have a question about multithreading download, as you know downloading using several threads improve performance of application, however there are some measures to respect: like the number of threads, the available bandwidth and some more, but I don't really understand, why the performance of application might be degraded by using many threads for example, or how can the bandwidth,quality of server affect the performance of multithreaded application? , what are the cases in which monothread download is faster than multithread?
Thanks for your replies.

推荐答案

我假设您是指下载管理器.

I assume you're referring to download managers.

首先,我对下载管理器真正提供多少性能"表示怀疑.但更重要的是,由于多线程,他们确实提供的任何好处是 不是 .下载的性能限制是连接的带宽.这就是为什么我对收益持怀疑态度的原因:

First, I'm sceptical of how much "performance" benefit a download manager really provides. But more importantly, any benefit they do provide is not due to multi-threading. The performance constraint of a download is the bandwidth of the connection. And this is why I'm sceptical of the benefits:

  • 1 Mbps连接将以1 Mbps下载.
  • 将文件分成4个段,意味着您以256 Kbps和4 * 256 Kbps = 1 Mbps的速度下载每个段.
  • 如果服务器限制每个下载段,则可能会有所改善.
  • 如果其中一个段超时,您可能会得到一点好处:其他段的下载意味着您​​的连接在超时等待期间不会处于空闲状态.
  • 您还可以通过淘汰"其他任何试图使用该连接的方式来加快下载速度. (并不是说我真的会称其为好处.)

下载管理器的真正好处在于有效地自动重新开始下载(即,如果可能的话,不从头开始重新启动).

The real benefit of a download manager is in automatically restarting downloads efficiently (i.e. not re-starting from scratch if possible).

让我们首先消除一个神话:多线程 可以加快任何速度.如果例程需要X个时钟周期才能运行:它将花费X个时钟周期;无论是1个线程还是多个线程.

Let's first dispel a myth: Multi-threading does not speed anything up. If a routine requires X clock-cycles to run: it will take X clock-cycles; whether on 1 thread or many threads.

多线程的作用是:它允许任务同时(同时)运行 .

What multi-threading does do: it allows tasks to run concurrently (at the same time).

能够同时做不同的事情意味着:

The ability to do different things at the same time means:

  • 可以在不同的线程上完成缓慢的任务(组合大量下载的各个部分),而不会干扰需要快速响应的其他线程(例如用户界面).
  • 并发任务还可以更有效地使用更多可用资源(多个CPU).请注意(回答问题的最后一部分),如果您只有一个CPU,则操作系统会对线程进行时间分割",因此它不是真正的并发.但是时间片很小,因此以前的好处仍然适用.

几乎总是在CPU不是瓶颈的情况下.对于下载:如前所述,瓶颈是连接的两个端点之间的带宽.实际上,许多线程意味着您需要做更多的工作(管理和协调不同的线程).

Well, pretty much always in cases where CPU is not the bottle-neck. In the case of download: As mentioned before, the bottle-neck is the bandwidth between the two end-points of the connection. Many threads actually means you have to do more work (managing and coordinating the different threads).

最有效的下载方法是2个线程:一个用于UI,另一个用于下载,这样任何暂停/执行都不会使用户界面停顿.

The most efficient approach for download is 2 threads: one for the UI, and the other for the download so that any pauses/dealys don't stall the user interface.

但是,更一般而言,即使您具有CPU密集型工作,理论上可以从多个线程同时执行不同工作中受益,也很容易犯实现上的错误,从而导致 实际使您的应用程序变慢 .

However, more generally even when you have CPU intensive work that could theoretically benefit from multiple threads doing different work concurrently, it's very easy to make mistakes in implementation that actually slow down your application.

  • 理想情况下,您的多个任务不应共享数据.因为如果这样做,您将冒竞赛条件并发性错误的风险.
  • 当他们必须共享数据时,您需要以某种方式同步工作,以避免上述错误. (有很多技术可以根据您的需要进行选择,在这里我将不做详细介绍.)
  • 但是,如果您的同步计划不周,您可能会面临引入许多问题的风险,这些问题可能会严重降低应用程序的速度.这些包括:
    • 对共享资源进行瓶颈处理,以使您的多个线程在任何情况下都无法同时运行.
    • 高锁争用,任务等待时间多于工作时间.
    • 即使死锁也可以完全阻止某些任务.
    • Ideally your multiple tasks should not share data. Because if they do, then you risk race-condition or concurrency bugs.
    • When they do have to share data, you need to synchronise the work in some way to avoid the above mentioned bugs. (There are many techniques to choose from depending on your needs and I won't go into detail here.)
    • However if your synchronisation is poorly planned you risk introducing a number of problems that can significantly slow down your application. These include:
      • Bottle-necking through a shared resource to make your multiple threads unable to run concurrently in any case.
      • High lock contention where task spend more time waiting than working.
      • Even deadlocking which can totally block some tasks.

      这篇关于关于多线程下载的缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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