当前位置:网站首页>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
边栏推荐
- LaTeX数学公式
- GUI编程简介 swing
- What is augmented reality technology? Where can it be used?
- Redis Desktop Manager for Mac
- [58] length of the last word [leetcode]
- MATLAB入门资料
- cadence的工艺角仿真、蒙特卡洛仿真、PSRR
- LLVM之父Chris Lattner:编译器的黄金时代
- After a circle, I sorted out this set of interview questions..
- Resource packaging dependency tree
猜你喜欢

OneFlow學習筆記:從Functor到OpExprInterpreter

汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告

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

GUI编程简介 swing

1099 establish binary search tree (30 points)

Introduction to matlab

Harbor enterprise image management system

小程序报错 :should have url attribute when using navigateTo, redirectTo or switchTab

MySQL查询两张表属性值非重复的数据

Notes d'apprentissage oneflow: de functor à opexprinterpreter
随机推荐
Harbor enterprise image management system
LGB, XGB, cat, k-fold cross validation
Cadence process angle simulation, Monte Carlo simulation, PSRR
汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
Use include in databinding
企业微信应用授权/静默登录
Production practice elk
MYCAT configuration
Open services in the bottom bar of idea
Go language self-study series | initialization of golang structure
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
基于ThinkPHP5版本TRC20-资金归集解决方案
Go language self-study series | golang structure as function parameter
Consensus Token:web3.0生态流量的超级入口
2022-04-22 openebs cloud native storage
The most concerned occupations after 00: civil servants ranked second. What was the first?
Latex mathematical formula
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
Latex paper typesetting operation
Error: cannot find or load main class