当前位置:网站首页>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
边栏推荐
- JMeter接口测试工具基础 — Badboy工具
- How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?
- Wechat applet rich text picture width height adaptive method introduction (rich text)
- Machine learning perceptron and k-nearest neighbor
- HBuilder制作英雄皮肤抽奖小游戏
- leetCode-2221: 数组的三角和
- Learn to use the phpstripslush function to remove backslashes
- SSM integration
- Difference between package type and basic type
- pycharm快捷键大全
猜你喜欢
随机推荐
【JS逆向分享】某个网站社区信息
抓包工具charles实践分享
5. dish management business development
numpy.linspace()
numpy.logical_or
学习使用php对字符串中的特殊符号进行过滤的方法
解决DBeaver SQL Client 连接phoenix查询超时
JMeter interface test tool foundation - sampler (II)
Normal equation
2. login and exit function development
Spark提交参数--files的使用
charles抓包工具使用教程
SQL Server AVG function rounding
微信小程序rich-text图片宽高自适应的方法介绍(rich-text富文本)
numpy.logical_and()
Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves
Appium automation test foundation - mobile end test environment construction (I)
【资源分享】2022年第五届土木,建筑与环境工程国际会议(ICCAEE 2022)
What you must know about distributed systems -cap
Leetcode-498: diagonal traversal









