multithreading 误解单线程和多线程编程的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10110660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:18:41  来源:igfitidea点击:

Misunderstanding the difference between single-threading and multi-threading programming

multithreadinglanguage-agnostic

提问by Eng.Fouad

I have a misunderstanding of the difference between single-threading and multi-threading programming, so I want an answer to the following question to make everything clear.

我对单线程和多线程编程的区别有一个误解,所以我想回答下面的问题,让一切都清楚。

Suppose that there are 9 independent tasks and I want to accomplish them with a single-threaded program and a multi-threaded program. Basically it will be something like this:

假设有 9 个独立的任务,我想用一个单线程程序和一个多线程程序来完成它们。基本上它会是这样的:

Single-thread:

单线程:

- Execute task 1
- Execute task 2
- Execute task 3
- Execute task 4
- Execute task 5
- Execute task 6
- Execute task 7
- Execute task 8
- Execute task 9

Multi-threaded:

多线程:

Thread1:

线程 1:

- Execute task 1
- Execute task 2
- Execute task 3

Thread2:

线程2:

- Execute task 4
- Execute task 5
- Execute task 6

Thread3:

线程3:

- Execute task 7
- Execute task 8
- Execute task 9

As I understand, only ONEthread will be executed at a time (get the CPU), and once the quantum is finished, the thread scheduler will give the CPU time to another thread.

据我了解,一次只会执行一个线程(获取 CPU),一旦量子完成,线程调度程序会将 CPU 时间分配给另一个线程。

So, which program will be finished earlier? Is it the multi-threaded program (logically)? or is it the single-thread program (since the multi-threading has a lot of context-switching which takes some time)? and why? I need a good explanation please :)

那么,哪个程序会更早完成呢?它是多线程程序(逻辑上)吗?还是单线程程序(因为多线程有很多上下文切换需要一些时间)?为什么?我需要一个很好的解释,请:)

回答by hvgotcodes

It depends.

这取决于。

How many cpus do you have? How much I/O is involved in your tasks?

你有几个cpu?您的任务涉及多少 I/O?

If you have only 1 cpu, and the tasks have no blocking I/O, then the single threaded will finish equal to or faster than multi-threaded, as there is overhead to switching threads.

如果您只有 1 个 cpu,并且任务没有阻塞 I/O,那么单线程的完成将等于或快于多线程,因为切换线程有开销。

If you have 1 cpu, but the tasks involve a lot of blocking I/O, you might see a speedup by using threading, assuming work can be done when I/O is in progress.

如果您有 1 个 cpu,但任务涉及大量阻塞 I/O,您可能会看到使用线程加速,假设在 I/O 进行时可以完成工作。

If you have multiple cpus, then you should see a speedup with the multi-threaded implementation over the single threaded, since more than 1 thread can execute in parallel. Unless of course the tasks are I/O dominated, in which case the limiting factor is your device speed, not cpu power.

如果您有多个 CPU,那么您应该会看到多线程实现比单线程实现的加速,因为可以并行执行 1 个以上的线程。当然,除非任务以 I/O 为主,在这种情况下,限制因素是您的设备速度,而不是 CPU 功率。

回答by Guffa

As I understand, only ONE thread will be executed at a time

据我了解,一次只会执行一个线程

That would be the case if the CPU only had one core. Modern CPUs have multiple cores, and can run multiple threads in parallel.

如果 CPU 只有一个内核,情况就会如此。现代 CPU 具有多个内核,并且可以并行运行多个线程。

The program running three threads would run almost three times faster. Even if the tasks are independent, there are still some resources in the computer that has to be shared between the threads, like memory access.

运行三个线程的程序运行速度几乎是原来的三倍。即使任务是独立的,计算机中仍有一些资源必须在线程之间共享,例如内存访问。

回答by sscheider

Assumption Set: Single core with no hyperthreading; tasks are CPU bound; Each task take 3 quanta of time; Each scheduler allocation is limited to 1 quanta of time; FIFO scheduler Nonpreemptive; All threads hit the scheduler at the same time; All context switches require the same amount of time;

假设集:单核,无超线程;任务受 CPU 限制;每个任务需要 3 个时间量;每个调度程序分配限制为 1 个时间量;FIFO 调度程序非抢占式;所有线程同时命中调度器;所有上下文切换都需要相同的时间;

Processes are delineated as follows:

流程描述如下:

  • Test 1: Single Process, single thread (contains all 9 tasks)
  • Test 2: Single Process, three threads (contain 3 tasks each)
  • Test 3: Three Processes, each single threaded (contain 3 tasks each)
  • Test 4: Three Processes, each with three threads (contain one task each)
  • 测试一:单进程单线程(包含全部9个任务)
  • 测试 2:单进程,三个线程(每个包含 3 个任务)
  • 测试 3:三个进程,每个进程都是单线程的(每个进程包含 3 个任务)
  • 测试 4:三个进程,每个进程有三个线程(每个进程包含一个任务)

With the above assumptions, they all finish at the same time. This is because there is an identicle amount of time scheduled for the CPU, context switches are identicle, there is no interrupt handling, and nothing is waiting for IO.

有了上面的假设,它们都同时完成了。这是因为为 CPU 安排了相同的时间量,上下文切换相同,没有中断处理,并且没有任何东西在等待 IO。

For more depth into the nature of this, please find this book.

如需更深入地了解其本质,请参阅本书

回答by Tim Bender

Well, this isn't entirely language agnostic. Some interpreted programming languages don't support real Threads. That is, threads of execution can be defined by the program, but the interpreter is single threaded so all execution is on one core of the CPU.

嗯,这并不完全是语言不可知的。一些解释型编程语言不支持真正的线程。也就是说,执行线程可以由程序定义,但解释器是单线程的,因此所有执行都在 CPU 的一个核心上。

For compiled languages and languages that support true multi-threading, a single CPU can have many cores. Actually, most desktop computers now have 2 or 4 cores. So a multi-threaded program executing truely independent tasks can finish 2-4 times faster based on the number of available cores in the CPU.

对于编译语言和支持真正多线程的语言,单个 CPU 可以有多个内核。实际上,现在大多数台式计算机都有 2 或 4 个内核。因此,基于 CPU 中可用内核的数量,执行真正独立任务的多线程程序可以快 2-4 倍的速度完成。

回答by Amarat

The main difference between single thread and multi thread in Java is that single thread executes tasks of a process while in multi-thread, multiple threads execute the tasks of a process.

Java中单线程和多线程的主要区别在于单线程执行一个进程的任务,而在多线程中,多个线程执行一个进程的任务。

A process is a program in execution. Process creation is a resource consuming task. Therefore, it is possible to divide a process into multiple units called threads. A thread is a lightweight process. It is possible to divide a single process into multiple threads and assign tasks to them. When there is one thread in a process, it is called a single threaded application. When there are multiple threads in a process, it is called a multi-threaded application.

进程是一个正在执行的程序。进程创建是一项资源消耗任务。因此,可以将一个进程划分为多个称为线程的单元。线程是一个轻量级的进程。可以将单个进程划分为多个线程并为其分配任务。当一个进程中有一个线程时,它被称为单线程应用程序。当一个进程中有多个线程时,称为多线程应用程序。

回答by jazst21

ruby vs python vs nodejs : performances in web app, which takes alot of I/O non blockingrest/dbQuery will impact alot. and being the only multi threaded of all 3, nodejs is the winner with big lead gap

ruby vs python vs nodejs:web 应用程序中的性能,需要大量 I/O 非阻塞rest/dbQuery 会影响很多。并且作为所有 3 个中唯一的多线程,nodejs 是领先差距很大的赢家