Multithreading: (priority, daemons)

1. Priority of thread

(1) Concept

A thread scheduler is provided to monitor all the threads in the ready state after the program is started. The thread scheduler determines which thread should be transferred for execution according to the priority. The priority of the thread is represented by a number, and the range is 1-10

(2) Procedure

public class PriorityTest implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"----"+Thread.currentThread().getPriority());
    }

    public static void main(String[] args) {
        PriorityTest priorityTest=new PriorityTest();
        System.out.println(Thread.currentThread().getName()+"----"+Thread.currentThread().getPriority());
        Thread thread1=new Thread(priorityTest);
        Thread thread2=new Thread(priorityTest);
        Thread thread3=new Thread(priorityTest);
        Thread thread4=new Thread(priorityTest);
        
        thread1.start();//5

        thread2.setPriority(1);//1
        thread2.start();

        thread3.setPriority(4);//4
        thread3.start();

        thread4.setPriority(Thread.MAX_PRIORITY);//10
        thread4.start();
    }
}

Test:

main----5
Thread-3----10
Thread-0----5
Thread-2----4
Thread-1----1

The default priority of the thread is 5. Low priority only means that the probability of getting scheduling is low, and it is not necessarily that the high priority is scheduled first

 

2. Daemons

Threads are divided into guard threads and user threads. The virtual machine must ensure that the execution of user threads is completed without waiting for the execution of guard threads (garbage collection)

(1) Define user thread:

public class You implements Runnable{
    @Override
    public void run() {
        for (int i=0;i<36500;i++){
            System.out.println("happy every day");
        }
        System.out.println("goodbye world!");
    }
}

(2) Define daemons:

public class God implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("God bless you");
        }
    }
}

(3) Test:

public class GodAndYouTest {
    public static void main(String[] args) {
        God god=new God();
        You you=new You();

        Thread thread=new Thread(god);
        thread.setDaemon(true);//The default is false´╝îIndicates that it is a user thread. Normal threads are all user threads
        thread.start();

        new Thread(you).start();//User thread start
    }
}

After setting the God thread as the guardian thread, start it, that is, set the setDaemon to false

goodbye world!
God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
 God bless you
Process finished with exit code 0

The daemonic thread is set as a dead loop, but the thread does not continue to loop, but after the user thread stops, the daemonic thread stops

Tags: Java

Posted on Thu, 28 May 2020 00:58:58 -0400 by Volatile