当前位置:网站首页>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

原网站

版权声明
本文为[bearstwu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130614013593.html