Open ericltw opened 6 years ago
In computer programming, single-threading is the processing of one command at a time. The opposite of single-threading is multithreading.
Multithreading is mainly found in multitasking operating system. Multithreading is widespread programming and execution model that allows multiple threads to exist within the context of one process. These threads share the process's resource, but are able to execute independently. The threaded programming model provides developers with a useful abstraction of concurrent execution. Multithreading can also be applied to one process to enable parallel execution on a multiprocessing system.
Multithreaded applications have the following advantages:
Multithreading has the following drawbacks:
Program
Process
Thread
是process的最小可執行單位。
當系統執行一個program時,系統創建一個process,而後開始執行該process的main thread。
一個process可以有多個thread,每個thread都有自己的工作。
Reference
Single thread
Multi-thread
Concurrency
實現Concurrency
Parallelism
Task switching
Reference
Sync / Async
描述code的執行流程。
Synchronous, Asynchronous為programming model。
Sync / Async與single thread / multi thread沒有對應關係。
What is a program
Program is an executable file containing the set of instructions written to perform a specific job on your computer. For example, chrome.exe is an executable file containing the set of instructions written so that we can view web pages. notpad.exe is an executable file containing the set of instructions which help us to edit and print the text files.
What is a process
Process is an executing instance of a program. For example, when you double click on Google Chrome icon on your computer, you start a process which will run the Google Chrome program. When you double click on a notepad icon on your computer, a process is started that will run the notepad program.
A process is sometimes refereed as active entity as it resides on the primary memory and leaves the memory if the system is rebooted. Several process may related to same program. For example, you can run multiple instance of a notepad program. Each instance is referred as a process.
What is a thread
Thread is the smallest executable unit of a process. For example, when you run a notepad program, operating system creates a process and starts the execution of main thread of the process.
A process can have multiple threads. Each thread will have their own task and own path of execution in a process. For example, in a notepad program, one thread will be taking user inputs and another thread will be printing a document.
All threads of the same process share memory of the that process. As threads of the same process share the same memory, communication between the threads is fast.
Processes and threads can be represented like below,
Process vs Thread
Reference