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

前端 未结 6 1407
梦毁少年i
梦毁少年i 2021-01-31 16:12

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.

相关标签:
6条回答
  • 2021-01-31 16:27

    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.

    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.

    0 讨论(0)
  • 2021-01-31 16:27

    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.

    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.

    0 讨论(0)
  • 2021-01-31 16:30

    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;

    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)

    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.

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

    0 讨论(0)
  • 2021-01-31 16:38

    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.

    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.

    0 讨论(0)
  • 2021-01-31 16:39

    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

    0 讨论(0)
  • 2021-01-31 16:51

    It depends.

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

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题