当前位置:网站首页>Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread
Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread
2022-04-23 09:03:00 【z754916067】
Catalog
Program process Basic concept of thread
Program
To accomplish a specific task , A collection of instructions written in a language . That is, a piece of static code , Static objects .
process
It's an execution of the program , Or a running program . It's a dynamic process : It has its own production , The process of existence and extinction , Life cycle .
Is the unit of resource allocation , At run time, the system allocates different memory areas for each process .
Threads
Processes can be further refined into threads , It's an execution path inside a program .
One Java Applications java.exe, There will be at least three threads :main( The main thread ) gc() Garbage collection thread , Exception handling threads
Thread as the unit of scheduling and execution , Every thread It has independent running stack and program counter , Low cost of thread switching .
Multiple threads in a process share the same memory unit / Memory address space -> They allocate objects from the same heap , You can access the same variables and objects . This makes inter thread communication easier and more efficient , However, the shared system resources operated by multiple threads may bring security risks .
Parallel and concurrent
parallel : Multiple CPU Perform multiple tasks at the same time
Concurrent : One CPU Perform multiple tasks at the same time
Memory structure diagram
Multiple threads share the method area and heap , Each thread has its own virtual machine stack and program counter
Thread creation and startup
Java Linguistic JVM Allow programs to run multiple threads , It passes through java.lang.Thread Class .
Thread Class
- Each thread passes through a particular Thread Object's run() Method to complete the operation , Often run() The body of the method is called the thread body .
- Through the Thread Object's start Method () To start this thread , Instead of calling directly run()
Mode one : Inherit Thread
- Create and inherit from Thread Subclasses of classes
- rewrite Thread Class run() Method , The operations that the thread needs to perform can be declared in run In the method .
- establish Thread Subclass object
- Call through this object start()
// Multithreading creation Mode one : Inherit Thread class
class MyThread extends Thread{
// rewrite run Method
@Override
public void run() {
for(int i=0;i<100;i++){
System.out.println(i);
}
}
}
public class ThreadTest {
public static void main(String[] args) {
MyThread myThread = new MyThread();
// Enable the current thread and call the current thread's run Method It can only be called once
myThread.start();
// The printed effect is and run() Mixed in the method This indicates that there are two threads executing
for(int i=0;i<100;i++){
System.out.println(i+"******main Threads ******");
}
}
}
Thread The common method of
start()
Start thread , And execute the object's run() Method
run()
The operation that a thread performs when it is scheduled
String getName()
Returns the name of the thread
void setName(String name)
Set the thread name
static Thread currentThread()
Returns the current thread . stay Thread In subclass is this, It is usually used for the main thread and Runnable Implementation class .
yield()
Thread concession , Pause the currently executing thread , Give the execution opportunity to threads with the same or higher priority .
join()
In a thread a Calling thread b Of join() Method , The thread a It's blocked , Until the thread b After full implementation , Threads a To end the blocking .
static void sleep(long millis)
Causes the current active thread to abandon within a specified time CPU control , Give other threads a chance to be executed , When the time is up, the current thread will start executing .
stop()
Force end of thread life cycle , Obsolete and not recommended
boolean isAlive()
return boolean, Determine if the thread is alive
版权声明
本文为[z754916067]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230722125272.html
边栏推荐
- [boutique] using dynamic agent to realize unified transaction management II
- 根据后序和中序遍历输出先序遍历 (25 分)
- Correct method of calculating inference time of neural network
- L2-024 tribe (25 points) (and check the collection)
- Failed to download esp32 program, prompting timeout
- cadence的工艺角仿真、蒙特卡洛仿真、PSRR
- 请提前布局 Star Trek突破链游全新玩法,市场热度持续高涨
- Arbre de dépendance de l'emballage des ressources
- 求简单类型的矩阵和
- The K neighbors of each sample are obtained by packet switching
猜你喜欢
Notes on xctf questions
1099 establish binary search tree (30 points)
2021李宏毅机器学习之Adaptive Learning Rate
汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
Enterprise wechat application authorization / silent login
LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
1099 建立二叉搜索树 (30 分)
PCTP考试经验分享
Chris LATTNER, father of llvm: the golden age of compilers
Introduction to matlab
随机推荐
Illegal character in scheme name at index 0:
论文阅读《Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry》
Consensus Token:web3. 0 super entrance of ecological flow
【原创】使用System.Text.Json对Json字符串进行格式化
Summary of solid problems
Open services in the bottom bar of idea
1099 establish binary search tree (30 points)
Search tree judgment (25 points)
Go language self-study series | golang method
Harbor enterprise image management system
玩转二叉树 (25 分)
LeetCode_DFS_中等_1254. 统计封闭岛屿的数目
L2-023 graph coloring problem (25 points) (graph traversal)
To remember the composition ~ the pre order traversal of binary tree
MATLAB入门资料
Whether the same binary search tree (25 points)
Introduction to matlab
Correct method of calculating inference time of neural network
Consensus Token:web3.0生态流量的超级入口
OneFlow学习笔记:从Functor到OpExprInterpreter