多线程编程和单线程编程
Multithreading is a popular term used in computing, operating systems, and application development areas. Multithreading is simply used to accelerate the execution of the process dividing into multiple sub-processes.
多线程是在计算,操作系统和应用程序开发领域中使用的流行术语。 多线程仅用于加速将进程分为多个子进程的执行。
什么是过程? (What Is Process?)
The process is the main unit used to execute the given task in the operating system. The process has some system resources like memory, CPU, disk, etc. in order to complete the given task. A single process can execute the only single task at a given time.
该过程是用于在操作系统中执行给定任务的主要单元。 该进程具有一些系统资源,如内存,CPU,磁盘等,以完成给定的任务。 单个进程可以在给定的时间执行唯一的单个任务。
什么是线程? (What Is Thread?)
Thread is a subprocess or tiny process which runs inside a process. Thread uses the process resources and provides the ability to run multiple tasks at the same time by using a given Central Processing Unit (CPU) cores or thread functions.
线程是在进程内运行的子进程或微小进程。 线程使用进程资源,并通过使用给定的中央处理单元(CPU)内核或线程功能来提供同时运行多个任务的能力。
什么是多线程? (What Is Multithread?)
Multithreading is the term used to run multiple threads inside a single process in order to execute multiple tasks at the same time. Multithreading can be done by using CPU threads via the operating system or using programming language ability to run multiple threads. Currently, most of the applications use multithreading in order to provide a better user experience.
多线程是用于在单个进程内运行多个线程以同时执行多个任务的术语。 可以通过操作系统使用CPU线程或使用编程语言来运行多个线程来完成多线程。 当前,大多数应用程序使用多线程以提供更好的用户体验。
多线程的使用 (Uses of Multithreading)
Multithreading can be used in different areas for different cases
在不同情况下,可以在不同区域使用多线程
- Today IT creates a lot of data which is very hard to processes. Single processes can not consume big data in a given time frame. We can use multithreading in order to dive the big data into multiple parts to processes with multiple threads 如今,IT部门会创建大量难以处理的数据。 在给定的时间范围内,单个进程无法消耗大数据。 我们可以使用多线程来将大数据分为多个部分,以使用多个线程进行处理
- While using a single applications it may provide a lot of checks, controls or background tasks that should be done while the user is using the applications. We can use multithreading in order to execute multiple tasks like a check, save, filter, update in a single application is real-time. 在使用单个应用程序时,它可能会提供许多检查,控件或后台任务,这些操作应在用户使用应用程序时完成。 我们可以使用多线程来实时执行多个任务,例如检查,保存,过滤,更新。
- Games require a lot of processing power where multiple threads can be used to accomplish different tasks and provide this processing power. 游戏需要大量的处理能力,其中可以使用多个线程来完成不同的任务并提供这种处理能力。
- In mobile systems like Android,iPhone there are a lot of services that need to run always and check new data, status from remote services. Multithreading can be used to run these multiple services. 在Android,iPhone等移动系统中,有很多服务需要始终运行并检查远程服务中的新数据和状态。 多线程可用于运行这些多个服务。
- Currently, web applications gained very interactive usage. Multithreading is used to run multiple tasks at the same time. 当前,Web应用程序获得了非常互动的使用。 多线程用于同时运行多个任务。
多线程的优点 (Advantages of Multithreading)
Multithreading provides different advantages for a system and application.
多线程为系统和应用程序提供了不同的优势。
- `Efficiency`: Creating a thread is very efficient where the same resources of the given process are used. 效率:在使用给定进程的相同资源的情况下,创建线程非常高效。
- `Resource Sharing`: As stated in the previous step the thread will use the given process resources which will make the resource usage lesser. “资源共享”:如上一步所述,线程将使用给定的进程资源,这将减少资源的使用。
- `Responsiveness`: By running multiple tasks at the same time the responsiveness can be improved. 响应能力:通过同时运行多个任务,可以提高响应能力。
- `Scalability`: A single big task can be divided into multiple little tasks and these tasks can be run in multiple threads which will scale the big task. 可伸缩性:单个大任务可以分为多个小任务,这些任务可以在多个线程中运行,这将扩展该大任务。
多线程类型 (Multithreading Types)
There is two main multithreading types or method. We call them pre-emptive
and cooperative` multithreading. These types are related to the context switching methodology of the threads.
有两种主要的多线程类型或方法。 我们称它们pre-emptive
和合作式多线程。 这些类型与线程的上下文切换方法有关。
先发制人 (Pre-emptive)
In pre-emptive multithreading, the operating system decides the context switching which means the pausing of the given thread and running other threads. For example, a low priority thread can be started by pausing a high priority thread.
在抢占式多线程中,操作系统决定上下文切换,这意味着暂停给定线程并运行其他线程。 例如,可以通过暂停高优先级线程来启动低优先级线程。
合作社 (Cooperative)
In cooperative multithreading, the context switching is controlled by the threads. The active thread will pause itself and swith to the next thread voluntarily. But this type of switching can create some deadlock because the threads can be managed and check the OS resources.
在协作多线程中,上下文切换由线程控制。 活动线程将自行暂停并自动切换到下一个线程。 但是这种类型的切换会造成一些死锁,因为可以管理线程并检查OS资源。
翻译自: https://www.poftut.com/what-is-multithreading-in-operating-systems-and-programming/
多线程编程和单线程编程