当前位置:网站首页>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
边栏推荐
- Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
- MySQL小練習(僅適合初學者,非初學者勿進)
- Consensus Token:web3. 0 super entrance of ecological flow
- Applet in wechat and app get current ()
- Trc20 fund collection solution based on thinkphp5 version
- 2021李宏毅机器学习之Adaptive Learning Rate
- Go language self-study series | golang structure pointer
- Experimental report on analysis of overflow vulnerability of assembly language and reverse engineering stack
- Project upload part
- 政务中台研究目的建设目标,建设意义,技术创新点,技术效果
猜你喜欢

Learn SQL injection in sqli liabs (Level 11 ~ level 20)

L2-024 部落 (25 分)(并查集)

npm ERR! network

2022-04-22 openebs cloud native storage

MySQL small exercise (only suitable for beginners, non beginners are not allowed to enter)

Four pictures to understand some basic usage of Matplotlib

First principle mind map

Valgrind and kcache grind use run analysis

Project upload part

Arbre de dépendance de l'emballage des ressources
随机推荐
完全二叉搜索树 (30 分)
Go language self-study series | golang structure as function parameter
Notes on xctf questions
Study notes of deep learning (8)
About CIN, scanf and getline, getchar, CIN Mixed use of getline
Judgment on heap (25 points) two insertion methods
LGB, XGB, cat, k-fold cross validation
【原创】使用System.Text.Json对Json字符串进行格式化
Brief steps to build a website / application using flash and H5
爬虫使用xpath解析时返回为空,获取不到相应的元素的原因和解决办法
OneFlow學習筆記:從Functor到OpExprInterpreter
Common errors of VMware building es8
Correct method of calculating inference time of neural network
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
Automatic differentiation and higher order derivative in deep learning framework
To remember the composition ~ the pre order traversal of binary tree
Bk3633 specification
MYCAT configuration
Go language self-study series | golang structure pointer