Guys, don't be silly to distinguish the thread life cycle and thread pool life cycle!!!


Thread life cycle

Source code proof

Calling thread getState method: Thread.currentThread().getState();

    public State getState() {
        // get current thread state
        return sun.misc.VM.toThreadState(threadStatus);
    }
    
    public static State toThreadState(int var0) {
        if ((var0 & 4) != 0) {
            return State.RUNNABLE;
        } else if ((var0 & 1024) != 0) {
            return State.BLOCKED;
        } else if ((var0 & 16) != 0) {
            return State.WAITING;
        } else if ((var0 & 32) != 0) {
            return State.TIMED_WAITING;
        } else if ((var0 & 2) != 0) {
            return State.TERMINATED;
        } else {
            return (var0 & 1) == 0 ? State.NEW : State.RUNNABLE;
        }
    }

It can be seen that State is an enumeration class

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

        /**
         * Thread state for a runnable thread.  A thread in the runnable
         * state is executing in the Java virtual machine but it may
         * be waiting for other resources from the operating system
         * such as processor.
         */
        RUNNABLE,

        /**
         * Thread state for a thread blocked waiting for a monitor lock.
         * A thread in the blocked state is waiting for a monitor lock
         * to enter a synchronized block/method or
         * reenter a synchronized block/method after calling
         * {@link Object#wait() Object.wait}.
         */
        BLOCKED,

        /**
         * Thread state for a waiting thread.
         * A thread is in the waiting state due to calling one of the
         * following methods:
         * <ul>
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
         *   <li>{@link #join() Thread.join} with no timeout</li>
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
         * </ul>
         *
         * <p>A thread in the waiting state is waiting for another thread to
         * perform a particular action.
         *
         * For example, a thread that has called <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
         * that object. A thread that has called <tt>Thread.join()</tt>
         * is waiting for a specified thread to terminate.
         */
        WAITING,

        /**
         * Thread state for a waiting thread with a specified waiting time.
         * A thread is in the timed waiting state due to calling one of
         * the following methods with a specified positive waiting time:
         * <ul>
         *   <li>{@link #sleep Thread.sleep}</li>
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
         *   <li>{@link #join(long) Thread.join} with timeout</li>
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
         * </ul>
         */
        TIMED_WAITING,

        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;
    }
  • New: a new thread object has been created, but the start() method has not been called yet.
  • Runnable: the two states of ready and running in Java threads are generally called "running".
    After the thread object is created, other threads (such as main thread) call the start() method of the object. The thread in this state is located in the runnable thread pool, waiting to be selected by thread scheduling to obtain the use right of CPU. At this time, it is in ready state. The thread in the ready state becomes running after obtaining the CPU time slice.
  • Blocked: the thread failed to acquire the lock and entered the blocked state
  • Waiting: the thread entering this state needs to wait for other threads to make some specific actions (notification or interrupt).
    • Call object wait (),Thread.join(),LockSupport.park enters the waiting state

  • Timed_WAITING: the thread state of the WAITING thread with the specified WAITING time. This state is different from WAITING. It can return after the specified time.
    • Call thread sleep ,Object.wait(long timeout),Thread.join(long millis) and others enter the waiting state of the supermarket

  • Terminated: indicates that the thread has completed execution.

Thread pool lifecycle

Source code proof

    private static final int RUNNING    = -1 << COUNT_BITS;
    private static final int SHUTDOWN   =  0 << COUNT_BITS;
    private static final int STOP       =  1 << COUNT_BITS;
    private static final int TIDYING    =  2 << COUNT_BITS;
    private static final int TERMINATED =  3 << COUNT_BITS;

1,RUNNING

  1. Status description: when the thread pool is in the RUNNING state, it can receive new tasks and process the added tasks.
  2. State switching: the initialization state of the thread pool is RUNNING. In other words, once the thread pool is created, it is in the RUNNING state, and the number of tasks in the thread pool is 0!
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));

2, ShutDown

  1. Status description: when the thread pool is in SHUTDOWN status, it does not receive new tasks, but can handle added tasks.

  2. State switching: when calling the shutdown() of the thread pool, the thread pool is changed from running - > shutdown.

3,STOP

  1. Status description: when the thread pool is in the STOP state, it will not receive new tasks, will not process added tasks, and will interrupt the tasks being processed.

  2. State switching: when calling the shutdown now() of the thread pool, the thread pool is controlled by (running or shutdown) - > stop.

4,tidying

  1. Status description: when all tasks have been terminated, the thread pool will change to TIDYING status. When the thread pool becomes TIDYING, the hook function terminated() is executed. Terminated() is empty in ThreadPoolExecutor class. If the user wants to change the thread pool to TIDYING, carry out corresponding processing; This can be achieved by overloading the terminated () function.

  2. State switching: when the thread pool is in SHUTDOWN state, the blocking queue is empty and the tasks executed in the thread pool are also empty, it will be switched from SHUTDOWN - > tidying.

When the thread pool is in the STOP state and the task executed in the thread pool is empty, it will be stopped - > tidying.

5, TERMINATED(terminated)

  1. Status description: when the thread pool is completely TERMINATED, it becomes TERMINATED.

  2. State switching: when the thread pool is in TIDYING state, after executing terminated(), it will be changed from TIDYING - > terminated.


CSDN exclusive benefits come!!!


Recently, CSDN has an exclusive activity, that is, the following "full stack knowledge map of Java". The route planning is very detailed. The size is 870mm x 560mm. Small partners can study systematically according to the above process. Don't take yourself to find a book to study casually like I did in the beginning. The foundation of systematic and regular learning is the most solid. In our business, "The foundation is not strong, the earth and the mountains shake" is particularly obvious.

Finally, if you are interested, you can buy it at your discretion and pave the way for your future!!!



Last words

I am CRUD sketch master , in the coming days, we will continue to update blog posts that are beneficial to you. We look forward to your attention!!!

It's not easy to create. If this blog is helpful to you, I hope you can praise and pay attention to me. Thank you for your support. I'll see you next time~~~

Sharing outline

Interview question column of large factory


Directory index of Java learning route from entry to grave


Open source crawler instance tutorial directory index

For more wonderful content sharing, please click Hello World (●'◡'●)

Keywords: Java Multithreading thread

Added by minou on Wed, 02 Feb 2022 20:04:56 +0200