This is the fourth day of my participation in Gwen Challenge

This article has participated in the weekend study program, click to see more details.

Java defines six thread states, and each thread can be in only one of them at any point in time. And for a single-core CPU, only one thread can be running at any time. Thread states are transformed by specific mechanisms and methods.

Six states of Java threads

Volatile int threadStatus = 0; volatile int threadStatus = 0; . The value of this variable is defined by an enumerated class inside Thread.

public enum State {
        /** * New status * threads not started after creation */
        NEW,

        /** * The running state * includes executing, and possibly waiting for the operating system to allocate execution time for it */
        RUNNABLE,

        /** * Block state * the state of a thread waiting for a critical lock to be blocked */
        BLOCKED,

        Threads are not allocated processor execution time and need to wait for other threads to explicitly wake up */
        WAITING,

        Threads are not allocated processor execution time, but do not wait to be explicitly woken up by other threads * after a certain amount of time, they are automatically woken up by the operating system */
        TIMED_WAITING,

        /** * End status * Thread exits or has completed execution */
        TERMINATED;
    }
Copy the code

Interrupt-free thread state transition

The simplest life cycle of a normal thread should go through these three processes: ** start > run > terminate **.

Initial (NEW)

  • When a thread object is created, the thread does not start immediately. Instead, it initializes the threadStatus variable to a NEW state.

Run (RUNNABLE)

  • As we all know, the way to start a Thread is to call Threadstart()methods
  • callstart()Method, a local method is eventually calledprivate native void start0();
  • In the JVM,start0()Method will eventually call the operating system’s thread-creation method
  • In the callThread.start()After that, the state of the thread will change toRUNNABLEstate
  • On a single-core CPU, only one thread can run at a time. The operating system determines which thread to start. At the JVM level, a thread in a RUNNABLE state does not mean it is running, only that it has been given the opportunity to run at any time. At the operating system level, the threads in the runnable state will wait for scheduling in a READY queue. The threads that finally enter the CPU are truly RUNNING, and those that are still in the READY queue are called READY.
  • Java does not distinguish between RUNNING and READY, calling both RUNNABLE

TERMINATED (TERMINATED)

  • When a thread is TERMINATED, the state of the thread is TERMINATED.
  • In aTERMINATEDState threads cannot be resurrected and will be thrown if forced to restart the threadjava.lang.IllegalThreadStateExceptionThe exception.

3 Thread Blocking

When two concurrent threads access a synchronized block of the same object, the thread that acquired the lock enters the RUNNABLE state. The other thread will be BLOCKED until the next time it acquires the object lock before it enters the RUNNABLE state again.

When multiple concurrent threads compete for the same object lock, the thread that acquired the lock enters the RUNNABLE state. All other objects are BLOCKED and placed in the lock object’s synchronization queue; When the thread that holds the lock releases the lock, it wakes up the lock object to synchronize all the threads in the queue, and those threads continue to try to grab the lock. And so on.

4 Wait (), join(), and park()

When you use wait(), join(), and park(), the thread enters a WAITING state. There will be some differences in the use of these three methods.

wait()

  • The wait() method is invoked actively by the running thread
  • When a running thread calls wait(), the lock object is released, the state becomes WAITING, and the thread enters the wait column of the lock object
  • When another thread that has acquired the lock object calls notify/notifyAll, the thread in the waiting queue is woken up
  • Threads that are awakened vie for the lock and enter the RUNNABLE state, while threads that are not locked enter the BLOCKED state and enter the lock object synchronization queue

join()

  • When another thread calls join(), the running thread is forced to exit the RUNNABLE state and enter WAITING
  • At the bottom of join(), the wait() method is called by the running thread, and the lock object is the thread itself
  • The notifyAll() method is automatically called when a thread that enters the running state after using the join() method to cut the queue is finished

park()

  • One thread callLockSupport.park()Method, the thread state changes from RUNNABLE to WAITING
  • Another thread callsLocksupport. unpark(Thread just Thread)The thread will return to RUNNABLE from WAITING

5.TIMED_WAITING

This part is easy enough. Add a timeout parameter to each of the above methods to make the thread TIMED_WAITING.