当前位置:网站首页>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
边栏推荐
猜你喜欢

MySQL小練習(僅適合初學者,非初學者勿進)

Strength comparison vulnerability of PHP based on hash algorithm

MySQL小练习(仅适合初学者,非初学者勿进)

L2-022 重排链表 (25 分)(map+结构体模拟)

Valgrind and kcache grind use run analysis

PLC的点表(寄存器地址和点表定义)破解探测方案--方便工业互联网数据采集

LaTeX论文排版操作

计算神经网络推理时间的正确方法

Failed to download esp32 program, prompting timeout

Production practice elk
随机推荐
[original] use system Text. JSON formats the JSON string
Data visualization: use Excel to make radar chart
2021李宏毅机器学习之Adaptive Learning Rate
cadence的工艺角仿真、蒙特卡洛仿真、PSRR
L2-022 重排链表 (25 分)(map+结构体模拟)
idea打包 jar文件
Error: cannot find or load main class
1099 establish binary search tree (30 points)
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
Failed to prepare device for development
Technological innovation in government affairs in the construction of Digital Government
LaTeX数学公式
Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing
資源打包關系依賴樹
Search tree judgment (25 points)
L2-022 rearrange linked list (25 points) (map + structure simulation)
Bk3633 specification
OneFlow学习笔记:从Functor到OpExprInterpreter
L2-023 图着色问题 (25 分)(图的遍历)
L2-3 romantic silhouette (25 points)