当前位置:网站首页>Multi thread learning III. classification of threads
Multi thread learning III. classification of threads
2022-06-12 05:17:00 【Refuel every day!】
Multi thread learning III , Classification of threads : The main thread 、 The guardian thread 、 Sub thread
List of articles
Mind mapping :

1. The main thread
from main What the thread starts is The main thread
So in Java Is there only one thread in a class in ? No, it isn't ,Java Naturally multithreaded , Even in a simple Hello World There are also many threads in the program .

2. Sprite threads
stay Java in , Threads can be simply divided into two types : User threads (User Thread)、 The guardian thread (Daemon Thread). In a Java In the program , Any one of the daemons is the whole JVM The nannies of all non Guardian threads in :
As long as the present JVM There is still any non - daemonic thread in the instance that has not ended , The daemons are all working ; Only when the last non - daemonic thread ends , The guardian thread follows JVM Finish the work together .Daemon Its function is to provide convenient services for other threads , The most typical application of daemons is GC ( Garbage collector ), It is a very competent guardian .
Let's give an example User threads sign out , The guardian thread Examples of forced closure :
Here's what's interesting , You need to start the user thread before starting the child thread
public class _9_daemon {
public static void main(String[] args) {
Thread t1=new CommonThread();
// A new task 、 Then bind to a thread
Thread t2 = new Thread(new MyDaemon());
// take t2 Set to daemons
t2.setDaemon(true);
// The correct approach , Set the daemon thread before the thread runs
t2.start();
t1.start();
}
}
// Custom thread , Inherited from Thread
class CommonThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(" Subscriber line number "+i+" This execution ");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// The task class
class MyDaemon implements Runnable{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println(" Subscriber line number "+i+" This execution ");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
From the running results, we can see that , When the user thread exits , The daemon thread also exits automatically
Daemon thread number 0 This execution
Subscriber line number 0 This execution
Daemon thread number 1 This execution
Subscriber line number 1 This execution
Subscriber line number 2 This execution
Daemon thread number 2 This execution
Daemon thread number 3 This execution
Subscriber line number 3 This execution
Daemon thread number 4 This execution
Subscriber line number 4 This execution
Daemon thread number 5 This execution
3. Thread priority
We can control the execution order of threads by setting the priority of threads , But we can't Precise control , Who is running first , This is actually a probability event , I'm not sure , Take a chestnut :
public class _10_Priority {
public static void main(String[] args) {
ShowTime task = new ShowTime();
Thread t1 = new Thread(task," Thread one ");
// Set the priority of the thread
t1.setPriority(1);
t1.start();
Thread t2 = new Thread(task, " Thread two ");
t2.setPriority(10);
t2.start();
}
}
class ShowTime implements Runnable{
@Override
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy year MM month dd Japan hh:mm:ss");
while (true){
System.out.println(Thread.currentThread().getName()+" The current time is :"+sdf.format(System.currentTimeMillis()));
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Let's take a look at the printed results :
The current time of thread 1 is :2022 year 01 month 27 Japan 02:15:55
The current time of thread 2 is :2022 year 01 month 27 Japan 02:15:55
The current time of thread 1 is :2022 year 01 month 27 Japan 02:15:56
The current time of thread 2 is :2022 year 01 month 27 Japan 02:15:56
The current time of thread 1 is :2022 year 01 month 27 Japan 02:15:57
The current time of thread 2 is :2022 year 01 month 27 Japan 02:15:57
The current time of thread 2 is :2022 year 01 month 27 Japan 02:15:58
The current time of thread 1 is :2022 year 01 month 27 Japan 02:15:58
The current time of thread 2 is :2022 year 01 month 27 Japan 02:15:59
The current time of thread 1 is :2022 year 01 month 27 Japan 02:15:59
The current time of thread 1 is :2022 year 01 month 27 Japan 02:16:00
The current time of thread 2 is :2022 year 01 month 27 Japan 02:16:00
Can you see if Follow strictly , It's just a matter of probability .
边栏推荐
- Qinglong wool - Kaka
- LabVIEW关于TDMS和Binary存储速度
- Parallelization of accelerated training tf data. Dataset generator
- BI 如何让SaaS产品具有 “安全感”和“敏锐感”(上)
- Walking "daily question" and "DP"
- Pupanvr hardware and software board side development environment configuration (4)
- File contains (regardless of suffix) Apache log remote file contains PHP encapsulated pseudo protocol:
- Advanced MySQL knowledge points (7)
- Accumulated temperature spatial distribution data, temperature distribution data, sunshine data, rainfall distribution, solar radiation data, surface runoff data, land use data, NPP data, NDVI data
- One dragon and one knight accompanying programmers are 36 years old
猜你喜欢

Memory protection

Development of video preview for main interface of pupanvr-ui

New year news of osdu open underground data space Forum

2022 "college entrance examination memory" has been packaged, please check!

Redis learning notes (continuously updating)

Interview must ask: summary of ten classic sorting algorithms

Enhanced vegetation index evi, NDVI data, NPP data, GPP data, land use data, vegetation type data, rainfall data

One dragon and one knight accompanying programmers are 36 years old

Acquisition of Lai data, NPP data, GPP data and vegetation coverage data

SQL injection upload one sentence Trojan horse (turn)
随机推荐
Qs100 at command mqtt access thingsboard
Walking "daily question" and "DP"
LabVIEW关于TDMS和Binary存储速度
How to quickly reference uview UL in uniapp, and introduce and use uviewui in uni app
[backtracking method] backtracking method to solve the problem of Full Permutation
Spatial distribution data of China's tertiary watershed / national new area distribution data /npp net primary productivity data / spatial distribution data of vegetation cover / land use data /ndvi d
Rv1109 lvgl UI development
Serial port oscilloscope_ port_ Setup of plotter secondary development environment (including QT setup)
12.26 exercise summary
Asp. Net core EF value conversion
Ten trends of Internet Security in 2022 industry released
Data processing and data set preparation
Ecosystem type distribution data, land use data, vegetation type distribution and nature reserve distribution data
Soil type, soil texture, soil nutrient and change data, soil organic matter, soil pH, soil nitrogen, phosphorus and potassium
1008 color classification
[backtracking based on bit operation] queen n problem 2
12.24 day exercise -- Programming summation, 99 multiplication table, while loop and for loop exercises
How Bi makes SaaS products have a "sense of security" and "sensitivity" (Part I)
CentOS compiling and installing mysql8.0
[backtracking] backtracking method to solve combinatorial problems