Thread Introduction 3–Thread Politeness: Yield; Thread Mandatory Execution: join; Observe Thread Status: state; Get and Set Thread Priority: getPriority, setPriority; Daemon Thread: Daemon

Introduction to threads

Thread courtesy

  • Thread politeness: thread politeness is to change the thread in the active state to the thread in the ready state, and wait for the scheduling of the CPU again at the same time as other threads in the ready state. politeness may not be successful.

  • Code Demonstration Understanding:

    1. Source code without courtesy:
package com.XianCheng.Yield;
// thread courtesy
public class Yield {<!-- -->
    public static void main(String[] args) {<!-- -->
        //Create thread object
        NoYield noYield = new NoYield();
        // call thread method
        new Thread(noYield,"a").start();
        new Thread(noYield,"b").start();
    }
}
//Create a thread without politeness
class NoYield implements Runnable{<!-- -->

    @Override
    public void run() {<!-- -->
        System.out.println(Thread.currentThread().getName() + "thread start");
        System.out.println(Thread.currentThread().getName() + "thread end");
    }
}

In the case of non-comity, it will give priority to the execution of the previous thread and then execute the next thread

  • result:
  • Do thread politeness:
    • source code:
//Create a thread and perform courtesy
class NoYield implements Runnable{<!-- -->

    @Override
    public void run() {<!-- -->
        System.out.println(Thread.currentThread().getName() + "thread start");
        Thread.yield();//Thread yield
        System.out.println(Thread.currentThread().getName() + "thread end");
    }
}

  • After politeness, the previous thread will have an active state to a ready state, and the thread in the ready state will be selected for execution by the CPU.
  • result:

Thread enforcement

  • Thread enforcement: join();
  • Thread enforcement is to give priority to the execution of a certain thread, and other threads must be executed after the execution of the thread.
  • Using thread enforcement may cause thread congestion.
  • Code: Set up a t thread and a main thread, when the main thread loops to 200, thread t will be enforced.
package com.XianCheng.ThreadJoin;
/*
  thread enforcement
    1. Join can directly interrupt the executing thread, and after executing the thread corresponding to the join first, execute the original thread
 */
public class Join implements Runnable {<!-- -->

    @Override
    public void run() {<!-- -->
        for (int i = 0; i <1000; i ++ ) {<!-- -->
            System.out.println("I am the thread executing the join method" + i);
        }
    }

    public static void main(String[] args) throws InterruptedException {<!-- -->
        //Create thread object
        Join join = new Join();
        //Start the thread
        Thread t=new Thread(join);
        t. start();
        //execute the main thread
        for (int i = 0; i < 500; i ++ ) {<!-- -->
            if (i==200){<!-- -->
                //Use join to enforce t thread
                t. join();
            }
            System.out.println("I am the main thread" + i);
        }
    }
}

Observe thread state=>state

  • The state of the thread: new, run, waiting
  • Get the current state of the thread by calling thread.state()
  • Can’t explain clearly, look at the code:
package com.XianCheng.ThreadState;
//Observe thread status
public class ThreadState {<!-- -->
    public static void main(String[] args) {<!-- -->
        //Create thread object
        Thread thread=new Thread(()->{<!-- -->
            for (int i = 0; i < 5; i ++ ) {<!-- -->
                try {<!-- -->
                    Thread. sleep(100);
                } catch (InterruptedException e) {<!-- -->
                    e.printStackTrace();
                }
            }
            System.out.println("//");
        });
        //Observe thread status
        Thread. State state = thread. getState();
        System.out.println(state);
        //Start the thread
        thread. start();
        //Get the status of thread startup
        state=thread. getState();
        System.out.println(state);
        //Observe the state after the thread stops
        //Thread.State.TERMINATED)=>Thread ends
        while (state!=Thread.State.TERMINATED){<!-- -->
            state=thread. getState();
            System.out.println(state);
        }
    }
}

Priority of threads

  • Get thread priority: thread.getPriority()
  • Set thread priority: thread.setPrioroty (priority number)
  • The priority of the thread is only that the one with the higher priority will be executed first in most cases, it does not mean that the one with the lower priority will be executed later.
  • The priority of the thread is between 1-10, and the program will report an error if it exceeds this range
  • Code to speak:
package com.XianCheng.Priority;
//Get thread priority and set thread priority
public class Priority {<!-- -->
    public static void main(String[] args) {<!-- -->
        // Get the thread priority of the main thread
        System.out.println(Thread.currentThread().getPriority());
        //Create thread object
        thread thread = new thread();
        Thread thread1=new Thread(thread);
        //Set thread priority
        thread1. setPriority(1);
        //Start the thread
        thread1. start();

        Thread thread2=new Thread(thread);
        //Set thread priority
        thread2. setPriority(10);
        //Start the thread
        thread2.start();
        Thread thread3=new Thread(thread);
        //Set thread priority
        thread3.setPriority(Thread.MIN_PRIORITY);
        //Start the thread
        thread3.start();
    }
}
//Create a thread class
class thread implements Runnable{<!-- -->
    @Override
    public void run() {<!-- -->
        System.out.println(Thread.currentThread().getName() + "The thread priority is -->" + Thread.currentThread().getPriority());
    }
}

Daemon thread

  • Threads are divided into: user threads and daemon threads
  • The virtual machine jvm does not need to wait for the execution of the daemon thread to complete, and the virtual machine can be shut down only after the execution of the non-daemon thread is completed.
  • Such as recording operation logs in the background, monitoring memory, garbage collection, etc. are all daemon threads
  • Set daemon thread: thread.setDaemon(thread name)
  • Code display: set a god thread as a daemon thread and let it loop continuously, set a human thread as a user thread, loop 500 times, and observe when the system stops running.
package com.XianCheng;

public class Deamon {<!-- -->
    public static void main(String[] args) {<!-- -->
        //Create daemon thread object
        god god = new god();
        Thread godThread=new Thread(god);
        godThread.setDaemon(true);//The default value is FALSE. The default thread is a user thread.
        //Start daemon thread
        godThread. start();
        //Create user thread
        you you = new you();
        Thread ypuThread=new Thread(you);
        //Start the user thread
        ypuThread.start();

    }
}
//Create daemon thread
class god implements Runnable{<!-- -->

    @Override
    public void run() {<!-- -->
        while (true){<!-- -->
            System.out.println("I am a daemon thread");
        }
    }
}
//Create user thread
class you implements Runnable{<!-- -->

    @Override
    public void run() {<!-- -->
        for (int i = 0; i < 500; i ++ ) {<!-- -->
            System.out.println("I am a user thread");
        }
    }
}