当前位置:网站首页>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

img

img

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

img

  • 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

  1. 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
  2. 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
  3. 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
原网站

版权声明
本文为[Sit at a sunny window and drink tea alone]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240926141525.html