当前位置:网站首页>Thread correlation point
Thread correlation point
2022-06-13 06:23:00 【bearstwu】
Various special methods are often encountered in threads , Although it is impossible to remember every way , But we should understand and remember the common methods
For example, get the current thread object , Its name or its name can be changed
static Thread currentThread() | Get the current thread object |
String getName() | Get thread object name |
void setName(String name) | Modify the thread object name |
These three methods are used for related operations , But most of the methods are easier to operate than the three
According to an example , There is such code in the demonstration code
class MyThread2 extends Thread {
public void run(){
for(int i = 0; i < 100; i++){
Thread currentThread = Thread.currentThread();
System.out.println(currentThread.getName() + "-->" + i);
}
}
}
In this code , Notice the seventh line currentThread Way The function of this code is to output the information that the code segment is now being called by that thread when macnian checks the program
You can use this code to verify
public class ThreadTest04 {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName());
}
}
Get its output is main, It can be determined that the relevant information is output , The reason for the name of the output thread is that I use getName() Method , Thus reducing the information that will be output inside
If I put one of them getName() After removal, the result is
Thread[main,5,main]
Then come back to this question , It is easy to understand all the outputs
As I said before, thread interrupts are used sleep, When there is a situation that sleep needs to be terminated on site, it is necessary to interrupt. This should also be mentioned
But in general, it is important .
、
public class ThreadTest08 {
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable2());
t.setName("t");
t.start();
// hope 5 Seconds later ,t Thread wakes up (5 Seconds later, the work in the main thread is finished .)
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Final break t Thread sleep ( This way of ending sleep depends on java Exception handling mechanism of .)
t.interrupt();
}
}
class MyRunnable2 implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "---> begin");
try {
// sleep 1 year
Thread.sleep(1000 * 60 * 60 * 24 * 365);
} catch (InterruptedException e) {
e.printStackTrace();
}
//1 It's not going to be implemented until two years later
System.out.println(Thread.currentThread().getName() + "---> end");
}
Here is the code of a big man , Rest assured that you will not cpu Occupy too long , Placing the code for a period of time pauses the run . Then you can take a good look at this code , If you will MyRunnable2 About China sleep This paragraph and the related deletion , Then you will find that the program will be processing 5 It ends in seconds .
public class ThreadTest04 {
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable2());
t.setName("t");
t.start();
// hope 5 Seconds later ,t Thread wakes up (5 Seconds later, the work in the main thread is finished .)
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Final break t Thread sleep ( This way of ending sleep depends on java Exception handling mechanism of .)
t.interrupt();
}
}
class MyRunnable2 implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "---> begin");
//1 It's not going to be implemented until two years later
System.out.println(Thread.currentThread().getName() + "---> end");
}
}
But here it is. sleep It doesn't seem to have been terminated , There are doubts here , If anyone has experience, you can write it in the comments
Here are a few other tips run The method does not exist when it is written throws, This involves java One of the y principle , A subclass cannot throw more exceptions than its parent class .
Last time I mentioned terminating threads , Now there are simpler ways , About to change the tag of the thread where you want to terminate it to false that will do . If you need a special value , Try to write this large paragraph in if Judging
边栏推荐
- Applet disable native top
- Echart柱状图:x轴显示value,y轴显示类别
- 微信小程序:基础复习
- Fidde breakpoint interception
- AI realizes "Resurrection" of relatives | old photo repair | old photo coloring, recommended by free app
- Time conversion'2015-04-20t11:12:00.000+0000 '
- App performance test: (II) CPU
- USB status error and its cause (error code)
- Wechat applet jumps to H5 page with parameters
- Logcat -b events and eventlogtags print the location correspondence of the events log in the code
猜你喜欢
The boys x pubgmobile linkage is coming! Check out the latest game posters
USB 0xc0000011 error
【2022高考季】作为一个过来人想说的话
js将文本转成语言播放
楊輝三角形詳解
AI realizes "Resurrection" of relatives | old photo repair | old photo coloring, recommended by free app
Explication détaillée du triangle Yang hui
免费录屏软件Captura下载安装
[solution] camunda deployment process should point to a running platform rest API
Detailed explanation of Yanghui triangle
随机推荐
线程池学习
Simple use of event bus
Custom view - extensible collapsexpendview
After clicking the uniapp e-commerce H5 embedded applet, the page prompts "the page iframe does not support referencing non business domain names"
USB debugging assistant
c语言对文件相关的处理和应用
自定义View —— 可伸展的CollapsExpendView
RN Metro packaging process and sentry code monitoring
Echart line chart: different colors are displayed when the names of multiple line charts are the same
App performance test: (III) traffic monitoring
Binary search
[MySQL] basic knowledge review
Solution: vscode open file will always overwrite the last opened label
ADB shell sendent debug input event
免费录屏软件Captura下载安装
[var const let differences]
Basic knowledge of knowledge map
楊輝三角形詳解
Not in the following list of legal domain names, wechat applet solution
[JS] handwriting call(), apply(), bind()