当前位置:网站首页>Process and multithreading
Process and multithreading
2022-06-24 10:32:00 【Sit at a sunny window and drink tea alone】
Processes and threads
process
- A program consists of instructions and data , But these instructions have to run , Read and write data , You have to load the instructions into CPU, Data is loaded into memory .
- In the process of instruction running, the disk is also needed 、 Network and other equipment . Processes are used to load instructions 、 Manage memory 、 management IO Of
When a program is run , Load the program's code from disk to memory , At this point, a process begins .
A process can be considered an instance of a program .
Most programs can run multiple instance processes at the same time ( For example, Notepad 、 drawing 、 The browser etc. ), Some programs can only start one instance process ( For example, Netease cloud music 、360 Security guard, etc )
Threads
- A process can be divided into one or more threads .
- A thread is a stream of instructions , Give instructions in a certain order to CPU perform
- Java in , Thread as the minimum scheduling unit , Process is the smallest unit of resource allocation . stay windows The middle process is inactive , Just as a container for threads
distinguish between
Processes are basically independent of each other , Threads exist in the process , It's a subset of the process
Processes have shared resources , Such as memory space , For internal threads to share
Inter process communication is more complex
- The process communication of the same computer is called IPC(Inter-process communication)
- Process communication between different computers , Need to go through the Internet , And abide by a common agreement , for example HTTP
Thread communication is relatively simple , Because they share memory in the process , An example is that multiple threads can access the same shared variable
Threads are lighter , The cost of thread context switching is generally lower than that of process context switching
Parallelism and concurrency
parallel
Single core cpu Next , The thread is actually Serial execution Of . There is a component in the operating system called task scheduler , take cpu Time slice of (windows
The next time slice is about 15 millisecond ) It's assigned to different programs , Just because cpu Between the lines ( The time slice is very short ) The switch is very fast , Human sense
Feel is Simultaneous running . In a word, it is : Micro serial , Macro parallel ,
It's usually Threads take turns using CPU This is called concurrency , concurrent


Multicore cpu Next , Every nucleus (core) Can schedule running threads , At this time, the thread can be parallel .

- Concurrent (concurrent) At the same time Answer (dealing with) Ability to do many things
- parallel (parallel) It's doing it at the same time (doing) Ability to do many things
Case study 1 : Asynchronous call
Asynchronous call
For the caller :
- Need to wait for the result to return , To keep running is to synchronize
- There is no need to wait for the result to return , To be able to continue is asynchronous
Design
- Multithreading can make method execution asynchronous ( That is, don't wait ) For example, when reading disk files , Suppose that the read operation takes a long time 5 Second , Such as
- If there is no thread scheduling mechanism , this 5 second cpu Nothing can be done , The rest of the code has to be suspended …
Case study 2 : Parallel execution
Case introduction
Make full use of multicore cpu The advantages of , Improve operational efficiency . As in the following scenario :
Calculation 1 cost 10 ms
Calculation 2 cost 11 ms
Calculation 3 cost 9 ms
Summary needs 1 ms
- If it's a serial execution , So the total time spent is 10 + 11 + 9 + 1 = 31ms
- But if it's a quad core cpu, Each core uses threads 1 Perform calculations 1, Threads 2 Perform calculations 2, Threads 3 Perform calculations 3, that 3 individual
- Threads are parallel , The time spent depends only on the running time of the longest thread , namely 11ms Finally, adding the summary time will only take 12ms
Need to be in multi-core cpu To improve efficiency , Single core is still executed in turn
summary
- Single core cpu Next , Multithreading can't actually improve the running efficiency of programs , Just to be able to switch between different tasks , Different threads take turns to use
cpu , Not a thread is always occupied cpu, Other threads can't work - Multicore cpu Can run multiple threads in parallel , But whether we can improve the running efficiency of the program depends on the situation
Some tasks , Well designed , Split the task , Parallel execution , Of course, it can improve the efficiency of the program . But not all calculations
Everything can be split ( Refer to the following 【 Amdal's law 】)
Not all tasks need to be split , If the purpose of the task is different , There's no point in talking about separation and efficiency - IO Operation does not occupy cpu, It's just that we usually copy files using 【 Blocking IO】, At this time, it's equivalent to thread, though not cpu, But it takes one
Wait for IO end , Not taking full advantage of threads . That's why there's the back 【 Non blocking IO】 and 【 asynchronous IO】 Optimize
边栏推荐
猜你喜欢

自定义kindeditor编辑器的工具栏,items即去除不必要的工具栏或者保留部分工具栏

Customize the toolbars of the kindeditor editor. Items removes unnecessary toolbars or retains some toolbars

leetCode-2221: 数组的三角和

How to customize sharing links in wechat for H5 web pages

Practice sharing of packet capturing tool Charles

88.合并有序数组

多线程的应用 - 提升效率

Leetcode-498: diagonal traversal

23. Opencv——图像拼接项目

Flink checkpoint and savepoint
随机推荐
分布式事务原理以及解决分布式事务方案
Uniapp implements the function of clicking to make a call
Distributed transaction principle and solution
Appium自动化测试基础 — 移动端测试环境搭建(一)
numpy. logical_ and()
线程池的状态
JMeter接口测试工具基础— 使用Badboy录制JMeter脚本
线程运行原理
希尔排序图文详解+代码实现
web网站开发,图片懒加载
机械臂速成小指南(一):机械臂发展概况
Machine learning - principal component analysis (PCA)
SSM integration
解决微信小程序rich-text富文本标签内部图片宽高自适应的方法
Thread pool execution process
Hill sorting graphic explanation + code implementation
百度网盘下载一直请求中问题解决
numpy.linspace()
Role of message queuing
6. package management business development