1. Initial (NEW) : A NEW thread object is created, but the start() method has not yet been called.


2. RUNNABLE: Java threads refer to the ready and running states as “RUNNABLE.”


After the thread object is created, other threads, such as the main thread, call the start() method of the object. A thread in this state is in the runnable thread pool, waiting to be selected by thread scheduler for CPU usage, and is in the ready state. A thread in the ready state becomes running after obtaining a CPU time slice.


3. BLOCKED: A thread is BLOCKED by a lock.


4. WAITING: A thread in this state is WAITING for some specific action (notification or interrupt) from another thread.


5. TIMED_WAITING: This state is different from WAITING. It can return after a specified time.

6. TERMINATED: Indicates that the thread is TERMINATED.






Thread state diagram







1. Initial status

Implementation of the Runnable interface and Thread inheritance can get a Thread class, a new instance out, the Thread into the initial state.

2.1. Ready state

  • Ready just means you’re eligible to run, and you’re always ready until the scheduler picks you up.
  • Call the start() method of the thread, which enters the ready state.
  • The current thread’s sleep() method ends, the other threads’ join() ends, and when user input is complete, one thread takes the object lock, and these threads enter the ready state.
  • When the current thread runs out of time, the yield() method of the current thread is called, and the current thread enters the ready state.
  • Once the thread in the lock pool has acquired the object lock, it enters the ready state.

2.2. Running status

The state of a thread when the thread scheduler selects a thread from the runnable pool as the current thread. This is also the only way a thread can get into a running state.

3. Blocked status

The blocking state is the state of a thread blocking when it enters a method or block of code modified by the synchronized keyword (which acquires the lock).

4. Waiting for

Threads in this state are not allocated CPU execution time, they wait to be explicitly woken up, or they wait indefinitely.

5. Wait time out

Threads in this state are not allocated CPU execution time, but do not have to wait indefinitely to be explicitly woken up by other threads; they wake up automatically after a certain amount of time.

6. Termination status

  • When a thread’s run() method completes, or the main() method completes, we consider it terminated. The thread object may be alive, but it is no longer a single thread executing. Once a thread terminates, it cannot be resurrected.
  • On a termination of the thread calls the start () method, which will be thrown. Java lang. IllegalThreadStateException anomalies.

Waiting queue

  • Obj lock must be acquired before invoking obj’s wait() and notify() methods, which must be written in synchronized(obj) code.
  • Steps and diagrams related to wait queues


  • 1. Thread 1 has obtained the lock of object A and is using object A.
  • 2. Thread 1 calls wait() on object A.
  • 3. Thread 1 releases the lock of object A and immediately enters the wait queue.
  • 4. Objects in the lock pool compete for the lock of object A.
  • 5. Thread 5 acquires the lock of object A, enters the synchronized block, and uses object A.
  • 6. Thread 5 calls notifyAll() of object A to wake up all threads, and all threads enter the synchronization queue. If thread 5 calls notify() on object A, A thread is woken up, and it is not known who will be woken up. The awakened thread enters the synchronization queue.
  • 7. Synchronized, where notifyAll() is located, ends, and thread 5 releases the lock of object A.
  • 8. The thread in the synchronous queue contends for the object lock, but it is not known when thread 1 will get it.

Synchronizing queue state

  • When the current thread wants to call the synchronization method of object A, it finds that the lock of object A is occupied by another thread, and the current thread enters the synchronization queue. In short, the synchronization queue is filled with threads that want to compete for the object lock.
  • When thread 1 is awakened by thread 2, thread 1 enters the synchronization queue to claim the object lock.
  • A synchronous queue is a concept that only exists in the context of synchronization. One object corresponds to one synchronous queue.

Comparison of several methods

  • Thread.sleep(long millis), the current Thread enters TIMED_WAITING state, but does not release the object lock. After millis, the Thread automatically wakes up and enters ready state. Action: The best way to give other threads a chance to execute.
  • Thread.yield(), which must be called by the current Thread. The current Thread gives up the CPU slice, but does not release the lock resource. The running state changes to the ready state, allowing the OS to select the Thread again. What it does: Allows threads of the same priority to take turns, but there is no guarantee that they will take turns. In practice, there is no guarantee that yield() will yield because the yielding thread may be selected again by the thread scheduler. Thread.yield() does not block. This method is similar to sleep(), except that the user cannot specify how long to pause.
  • The current thread is in WAITING/TIMED_WAITING state, and the current thread will not release the object lock it has already held. The current thread enters the ready state when thread T completes execution or millis time expires.
  • Obj.wait (), the current thread calls the object’s wait() method, and the current thread releases the object lock and enters the wait queue. Wake up by notify()/notifyAll() or wait(long timeout) timeout time to automatically wake up.
  • Obj.notify () wakes up a single thread waiting on this object’s monitor, and the selection is arbitrary. NotifyAll () wakes up all threads waiting on the monitor of this object.