This is the sixth day of my participation in the More text Challenge. For details, see more text Challenge

introduce

A Java thread can only be in one of six different states at any given time in its running life cycle

The name of the state instructions
NEW In the initial state, the thread is built, but the start() method has not yet been called
RUNNABLE Running states,Java threads refer to both ready and running states in the operating system as “running”
BLOCKED Blocked, indicating that the thread is blocked from the lock
WAITING Wait state, indicating that the thread is in the wait state, indicating that the current thread needs notification from another thread (notify or notifyAll).
TIME_WAITING Timeout wait state. You can specify the wait time to return itself
TERMINATED Terminated status: Indicates that the current thread has completed execution

See that the Thread class defines a State enumeration type

public enum State {
        /** * Thread state for a thread which has not yet started. */
        NEW,


        RUNNABLE,

        BLOCKED,

        
        WAITING,


        TIMED_WAITING,


        TERMINATED;
    }
Copy the code

Demo in various states

NEW

    public void test1(a) { //NEW
        Thread t1 = new Thread(() -> {
            System.out.println("Thread1~~~~");
        });
        System.out.println(t1.getState());
    }
Copy the code

At this point, the thread has just been created and the start() method has not been called, so the state is NEW

TERMINATED

    @Test
    public void test2(a) throws InterruptedException { //TERMINATED
        Thread t1 = new Thread(() -> {
            System.out.println("The thread begins to execute");
            System.out.println("Thread terminates execution");
        });
        t1.start();
        Thread.sleep(3000);
        System.out.println(t1.getState());
    }
Copy the code

The state of the thread is checked at TERMINATED. Sleep is added to ensure that the thread is TERMINATED at getState

RUNNABLE

    @Test
    public void test3(a) { //RUNNABLE
        Thread t1 = new Thread(() -> {

        });
        t1.start();
        System.out.println(t1.getState());
    }
Copy the code

The thread is in the RUNNABLE state after the start() method has been called.

Note: After a thread is created, it will be in the NEW state. After the start() method is called, the thread will be in the READY state. The runnable thread is in the RUNNING state when it gets the CPU timeslice. The operating system hides the READY and RUNNING states in the Java Virtual Machine (JVM); it can only see the RUNNABLE state. So the Java system calls these two states RUNNABLE states.

BLOCKED

Here, we simulate a scene where we simulate finding a table for dinner. Student 1 and student 2 compete for a seat at the same time. Student 1 gets the seat first, and student 2 is in a blocked state and can only wait for student 1 to finish the meal

public class Test1 {
    public static void main(String[] args) throws InterruptedException {
        System.out.println(Thread.currentThread().getName());
        Table table = new Table();
        Thread student1 = new Thread(() -> {
            table.use();
        }, "s1");
        Thread student2 = new Thread(() -> {
            table.use();
        }, "s2");
        student1.start();
        Thread.sleep(1000);
        student2.start();
        Thread.sleep(500); System.out.println(student2.getState()); }}class Table { 
    public synchronized void use(a) {
        System.out.println(Thread.currentThread().getName() + "- Use the table.");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "Dinner is over."); }}Copy the code

WAITING

Here, we simulate student 1 to find that he forgot to order food after getting a seat, so he can only give up the resource of seat. Enter the Wait state.

public class Test2 {
    public static void main(String[] args) throws Exception {
        System.out.println(Thread.currentThread().getName());
        Table1 table = new Table1();
        Thread student1 = new Thread(() -> {
            try {
                table.use();
            } catch(Exception e) { e.printStackTrace(); }},"s1");
        student1.start();
        Thread.sleep(100); System.out.println(student1.getState()); Thread.currentThread().getState(); }}class Table1 { 
    public synchronized void use(a) throws Exception {
        System.out.println(Thread.currentThread().getName() + "- Use the table.");
        // I forgot to order
        System.out.println("Forgot to order.");
        wait(100);
        Thread.sleep(2000);
        System.out.println(Thread.currentThread().getName() + "Dinner is over."); }}Copy the code

TIME_WAITING

This adds an automatic return time to WAITING, even if no other thread tells it to return after a certain time.