多线程如何加快应用程序的速度(当线程无法同时运行时)? [英] How can multithreading speed up an application (when threads can't run concurrently)?

查看:93
本文介绍了多线程如何加快应用程序的速度(当线程无法同时运行时)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习多线程,但是在阅读了一些教程之后,我有点困惑. 我不了解多线程如何加快应用程序的速度.

I'm learning about multithreading, but after reading some tutorials I'm sort of confused. I don't understand how multithreading can speed up an application.

凭直觉,我想说多线程会使应用程序变慢,因为您必须不断等待这些信号量.

By intuition, I would say multithreading slows down an application, because you constantly have to wait for those semaphores.

当线程不能同时运行时,多线程如何以及何时可以加速应用程序?

How and when can multithreading speed up an application, when threads can't run concurrently?

推荐答案

我可以想到两种方式,第一种可能是您所说的并行线程".

Two ways I can think of, the first of which is probably what you mean by "parallel threading".

  • 如果您有多个CPU或内核,则在运行多个线程时它们可以同时工作.
  • 在单核情况下,如果线程最终等待(同步)I/O,则可以说您调用read()从磁带读取100 MB,则可以在等待时安排另一个线程并完成工作.

这篇关于多线程如何加快应用程序的速度(当线程无法同时运行时)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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