当前位置:网站首页>Basic concepts of multithreading (concurrency and parallelism, threads and processes) and entry cases
Basic concepts of multithreading (concurrency and parallelism, threads and processes) and entry cases
2022-04-23 04:57:00 【Have a drink together】
List of articles
Program without jump statement , They are carried out from top to bottom , Now I want to design a program , Playing games and listening to music , How to design ?
To solve the above problems , We have to use multiple processes or threads to solve .
Concurrency and parallelism
- Concurrent : Two or more events in Within the same time period happen .
- parallel : Two or more events in At the same time happen ( At the same time ).
In the operating system , Multiple programs installed , Concurrency means that there are multiple programs running at the same time in a period of time , This is on the list CPU In the system , Only one program can be executed at a time , That is to say, these programs run alternately in time-sharing , It just feels like it's running at the same time , That's because the time-sharing operation is very short .
And in many CPU In the system , Then these programs that can be executed concurrently can be allocated to multiple processors (CPU), Implement multitask parallel execution , That is, using each processor to process a program that can be executed concurrently , So that multiple programs can be executed at the same time . Multi-core in the current computer market CPU, It's a multi-core processor , nucleus The more , More programs to process in parallel , Can greatly improve the efficiency of computer operation .
Be careful : A computer with a single core processor can't handle multiple tasks in parallel , It can only be multiple tasks in a single CPU Run concurrently on . Empathy , Threads are the same , Understand that threads run in parallel from a macro perspective , But from the micro point of view, it is a serial operation , That is to say, one thread and one thread run , When there is only one system CPU when , Threads execute multiple threads in a certain order , We call this situation thread scheduling .
Threads and processes
- process : An application running in memory , Each process has a separate memory space , One application can run multiple processes at the same time ; A process is also an execution of a program , It is the basic unit of system operation program ; The system runs a program that is a process created from 、 Run to die .
- Threads : A thread is an execution unit in a process , Responsible for the execution of programs in the current process , At least one thread in a process . There can be multiple threads in a process , This application can also be called a multithreaded program .
In short : At least one process after a program runs , A process can contain multiple threads
We can use the taskbar at the bottom of the computer , Right click -----> Open Task Manager , Can view the progress of the current task :
process
Threads
Thread scheduling :
-
Time sharing scheduling
All threads rotate CPU Right to use , Average per thread CPU Time for . -
preemptive scheduling
Give priority to high priority threads CPU, If the threads have the same priority , Then we will randomly choose one ( Thread randomness ),Java Preemptive scheduling is used .- Set the priority of the thread
- Set the priority of the thread
-
Details of preemptive scheduling
Most operating systems support concurrent running of multiple processes , Today's operating systems almost support running multiple programs at the same time . such as : Now let's use the editor in class , While using the recording software , At the same time, the drawing board is on ,dos Windows and other software . here , These programs are running at the same time ,” It feels like the software is running at the same time “.
actually ,CPU( a central processor ) Using preemptive scheduling mode to switch between multiple threads at high speed . about CPU For a core of , Some time , Only one thread can be executed , and CPU The speed of switching between multiple threads is faster than our feeling , It seems to run at the same time .
Actually , Multithreaded program can not improve the running speed of program , But it can improve the efficiency of program operation , Give Way CPU Higher usage of .
Create thread class
Java Use java.lang.Thread
Class representative Threads , All thread objects must be Thread An instance of a class or its subclass . The function of each thread is to complete certain tasks , In fact, it is to execute a program flow, that is, a piece of sequential code .Java Use thread executor to represent this program flow .Java Through inheritance Thread Class to establish and Start multithreading The steps are as follows :
- Definition Thread Subclasses of classes , And override the run() Method , The run() The method body of the method represents the tasks that the thread needs to complete , So the run() Method is called thread executor .
- establish Thread Instances of subclasses , The thread object is created
- Calling the start() Method to start the thread
The code is as follows :
Test class :
public class Demo01 {
public static void main(String[] args) {
// Create a custom thread object
MyThread mt = new MyThread(" New Threads !");
// Start a new thread
mt.start();
// Execute... In the main method for loop
for (int i = 0; i < 10; i++) {
System.out.println("main Threads !"+i);
}
}
}
Custom thread class :
public class MyThread extends Thread {
// Define the construction method of the specified thread name
public MyThread(String name) {
// Calling the String Parameter construction method , Specify the name of the thread
super(name);
}
/** * rewrite run Method , Complete the logic of the thread execution */
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(getName()+": Being implemented !"+i);
}
}
}
版权声明
本文为[Have a drink together]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230453397136.html
边栏推荐
- JS détermine si la chaîne de nombres contient des caractères
- Informatics Aosai yibentong 1212: letters | openjudge 2.5 156: Letters
- Unity camera rotation with sliding effect (rotation)
- Gets all dates between two times
- Making message board with PHP + MySQL
- 多线程基本概念(并发与并行、线程与进程)和入门案例
- Innovation training (XII) reptile
- C# List字段排序含有数字和字符
- Excel protects worksheets and workbooks from damage
- ApplicationContext injection bean
猜你喜欢
2022/4/22
View analysis of scenic spots in ArcGIS
用LCR表完美测试无线充电系统中的线圈
Use the built-in function of win to transfer files between two computers in the same LAN (the speed is the same as that between local disks)
[database] MySQL single table query
跨境电商 | Facebook 和 Instagram:哪个社交媒体更适合你?
Unity RawImage背景无缝连接移动
Spark optimization
Field injection is not recommended using @ Autowired
多线程基本概念(并发与并行、线程与进程)和入门案例
随机推荐
【数据库】表的查看、修改和删除
Windows remote connection to redis
C. Tree Infection(模拟+贪心)
L2-011 play binary tree (build tree + BFS)
Innovation training (VII) FBV view & CBV view
Informatics Olympiad 1955: [11noip popularization group] Swiss round | openjudge 4.1 4363: Swiss round | Luogu p1309 [noip2011 popularization group] Swiss round
Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
Innovation training (IX) integration
Implementation of switching windows and capturing data in selenium mode
Graduation project
Unity3D 实用技巧 - 理论知识库(一)
使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘
scp命令详解
Unity摄像头跟随鼠标旋转
Custom switch control
unity摄像机旋转带有滑动效果(自转)
Spark case - wordcount
Com alibaba. Common methods of fastjson
Innovation training (II) task division
Case of using stream load to write data to Doris