Java learning summary: 27

Common operation methods of multithreading

Thread naming and acquisition

Because the state of multithreading is uncertain, the name of the thread becomes the only distinguishing mark. When defining the thread name, you must set the name before the thread starts. Try not to duplicate the name, and try not to modify the thread that has been started. first name.
Due to the uncertain state of the thread, every time you can operate a thread that is executing the run () method, then the method to obtain the current thread object is: public static Thread current Thread ().
Insert picture description here
Example: Observe the naming of threads

package Project.Study.Multithreading;
class MyTread4 implements Runnable{
    @Override
    public void run(){
        System.out.println(Thread.currentThread().getName());//使用currentThread()取得当前线程对象后再取得具体的线程名字
    }
}
public class Test4 {
    public static void main(String [] args) throws Exception{
        MyTread4 mt=new MyTread4();
        new Thread(mt,"自己的线程A").start();
        new Thread(mt).start();
        new Thread(mt,"自己的线程B").start();
        new Thread(mt).start();
        new Thread(mt).start();
    }
}
//结果:
//自己的线程A
//Thread-1
//自己的线程B
//Thread-0
//Thread-2

Example: Get the thread name

package Project.Study.Multithreading;
class MyTread4 implements Runnable{
    @Override
    public void run(){
        System.out.println(Thread.currentThread().getName());//使用currentThread()取得当前线程对象后再取得具体的线程名字
    }
}
public class Test4 {
    public static void main(String [] args) throws Exception{
        MyTread4 mt=new MyTread4();
        new Thread(mt,"自己的线程A").start();
        new Thread(mt).start();
        mt.run();
    }
}
//结果:
//main
//自己的线程A
//Thread-0

Through this program we can find that the main method itself also belongs to a thread.
Each JVM process starts at least the following two threads:
1. main thread: the main execution of the program, and start the subroutine;
2. gc thread: responsible for garbage collection.

Thread sleep

Thread sleep refers to making the execution speed of the program slower. The sleep operation method of the thread in the Thread class is: public static void sleep (long millis) throws InterruptedException, and the set sleep unit is milliseconds (ms).
Example: Observe the sleep characteristics

package Project.Study.Multithreading;

class MyThread5 implements Runnable{
    @Override
    public void run(){
        for (int x=0;x<10;x++){
            try {
                Thread.sleep(1000);     		//每次执行休眠1s
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+",x="+x);
        }
    }
}
public class Test5 {
    public static void main(String []args){
        MyThread5 mt1=new MyThread5();
        MyThread5 mt2=new MyThread5();
        MyThread5 mt3=new MyThread5();
        new Thread(mt1,"自己的线程对象A").start();
        new Thread(mt2,"自己的线程对象B").start();
        new Thread(mt3,"自己的线程对象C").start();
    }
}
//结果:
//自己的线程对象B,x=0
//自己的线程对象A,x=0
//自己的线程对象C,x=0
//自己的线程对象A,x=1
//自己的线程对象B,x=1
//自己的线程对象C,x=1
//自己的线程对象B,x=2
//自己的线程对象A,x=2
//自己的线程对象C,x=2
//...
//自己的线程对象C,x=9
//自己的线程对象B,x=9
//自己的线程对象A,x=9

The result of the above program is three and three outputs (actually an illusion). Three thread objects are output every second. Because the thread switching speed is too fast, there will be a way that all threads enter the run () method at the same time. The feeling, but in fact there is a gap.

Thread priority

In Java's thread operation, the thread with the highest priority may be executed first. The priority of the thread is shown below: Thread priority
Thread priority operation

No. Method or constant Types of description
1 public static final int MAX_PRIORITY constant Highest priority, value is 10
2 public static final int NORM_PRIORITY constant Medium priority, value 5
3 public static final int MIN_PRIORITY constant The lowest priority, the value is 1
4 public final void setPriority(int new Priority) ordinary Set thread priority
5 public final int getPriority(); ordinary Get thread priority

Example: Set thread priority

package Project.Study.Multithreading;

class MyThread6 implements Runnable{
    @Override
    public void run(){
        for(int x=0;x<20;x++){
            try {
                Thread.sleep(100);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+",x="+x);
        }
    }
}
public class Test6 {
    public static void main(String [] args){
        MyThread6 mt1 = new MyThread6();
        Thread t1 = new Thread(mt1,"自己的线程对象A");
        Thread t2= new Thread(mt1,"自己的线程对象B");
        Thread t3=new Thread(mt1,"自己的线程对象C");
        t1.setPriority(Thread.MIN_PRIORITY);
        t2.setPriority(Thread.MAX_PRIORITY);
        t3.setPriority(Thread.NORM_PRIORITY);
        t1.start();
        t2.start();
        t3.start();
    }
}
//结果:
//自己的线程对象C,x=0
//自己的线程对象A,x=0
//自己的线程对象B,x=0
//自己的线程对象A,x=1
//自己的线程对象C,x=1
//自己的线程对象B,x=1
//自己的线程对象C,x=2
//自己的线程对象A,x=2
//自己的线程对象B,x=2
//自己的线程对象B,x=3
//自己的线程对象C,x=3
//自己的线程对象A,x=3
//自己的线程对象B,x=4
//自己的线程对象A,x=4
//自己的线程对象C,x=4
//...
//自己的线程对象C,x=19
//自己的线程对象B,x=19
//自己的线程对象A,x=19

It can be seen from the running results that the execution order of the threads may not necessarily be executed according to the set priority, because the threads may not necessarily obtain cpu resources.

Inquiry: Priority of the main method

public class Test6 {
    public static void main(String [] args){
        System.out.println(Thread.currentThread().getPriority());
    }
}
//结果:
//5

It can be seen that the priority of the main method is 5

49 original articles published · Liked 25 · Visits 1521

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/105000852