当前位置:网站首页>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
边栏推荐
猜你喜欢
COM in wine (2) -- basic code analysis
Spark small case - RDD, spark SQL
JS engine loop mechanism: synchronous, asynchronous, event loop
《2021多多阅读报告》发布,95后、00后图书消费潜力攀升
拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
深度学习笔记 —— 微调
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
Introduction to raspberry pie 3B - system installation
Sword finger offer: the median in the data stream (priority queue large top heap small top heap leetcode 295)
Learning Android II from scratch - activity
随机推荐
L2-011 play binary tree (build tree + BFS)
Introduction to raspberry pie 3B - system installation
Sword finger offer: the path with a certain value in the binary tree (backtracking)
PHP counts the number of files in the specified folder
Innovation training (IV) preliminary preparation - server
JS détermine si la chaîne de nombres contient des caractères
Other problems encountered in debugging fingerprints
Better way to read configuration files than properties
Progress of innovation training (III)
泰克示波器DPO3054自校准SPC失败维修
Spark FAQ sorting - must see before interview
Spark small case - RDD, spark SQL
Agile practice | agile indicators to improve group predictability
Unity3d practical skills - theoretical knowledge base (I)
How can continuous integration (CI) / continuous delivery (CD) revolutionize automated testing
Jetpack -- lifecycle usage and source code analysis
HRegionServer的详解
Pixel mobile phone brick rescue tutorial
The programmer starts the required application with one click of window bat
《2021多多阅读报告》发布,95后、00后图书消费潜力攀升