Reprinted from: mp.weixin.qq.com/s/VDFANBzOG…

Thread state

Let’s start with an opening four questions

  • JavaHow many thread states are there?
  • JavaHow does thread state transition?
  • JavaWhat happens when a thread state transition is queued for a lock?
  • JavaWhen does a thread state transition enter the lock synchronization queue?

When you think of Java thread state, many readers immediately think of thread state transition diagrams, but when they think about it, they are not impressed. They only remember a few states.

Ask you to describe how Java thread state transitions, and your mind will probably be like the picture below, already a mess.

Don’t panic, a star in order to help readers to clarify the above problem, will step by step to draw the thread state transition diagram, so that readers really understand the Java thread state transition.

First, the states of threads can be divided into 6 states or 7 states, the specific states are as follows

Six states

  • New: New state
  • Runnable: Indicates the running status
  • Terminated: Termination state
  • Waiting: Wait state
  • TimedWaiting: Indicates the timeout wait status
  • Blocked: Blocked state

7 state

  • New: New state
  • Ready: Ready state
  • Running: Running status
  • Terminated: Termination state
  • Waiting: Wait state
  • TimedWaiting: Indicates the timeout wait status
  • Blocked: Blocked state

There is not much difference between state 6 and state 7, except that state 7 splits the Runnable state into Ready state and Running state.

A xing takes 7 states as an example to explain how they are transformed step by step.

New State (New)

We can declare a Thread class by implementing the Runnable interface or by inheriting Thread. After a new instance is created, the Thread enters the new state.

A newly born thread is in the new state.

Ready state

After the thread object is successfully created, the start() function of the thread is called, and the thread enters the ready state. The thread in this state enters the runnable thread pool, waiting to obtain the right to use the C P U.

The thread says, I’m ready, I’m ready, pick me

Running Status

At this point, the thread scheduler is selecting a thread from the runnable thread pool, and the thread enters the running state.

In other words, the thread gets the C, P, U time slice.

That’s not all. When the thread’s time slice runs out or the yield() function is called, the thread returns to the ready state.

As a lucky thread, I went into the running state, but my luck ran out and I had to go back to the ready state.

Terminated state (Terminated)

A thread continues to run until the end of execution or an unexpected termination during execution causes the thread to enter the terminated state.

Once a thread terminates, it cannot be resurrected. This is an irreversible process.

Thread’s life is coming to an end. It can be a smooth sailing life, or it can die young and sadly.

Waiting state

A running thread executes wait(), join(), locksupport.park (), and enters the wait state.

The wait() and join() functions cause the JVM to put the thread on the lock wait queue.

Threads in this state are not allocated CPU execution time, they wait to be actively awakened, otherwise they stay in a waiting state.

What if we want to wake up the thread?

The locksupport.unpark (t) function wakes up the specified thread, which returns to the ready state.

The notify(), notifyAll(), and join thread completion modes wake up the thread waiting for the lock queue, and the thread leaving the queue returns to the ready state.

Thread’s life ushered in disaster, listen to the words of the villain, ran to the Philippines to do work, the results were black factory buckle, only hope the police uncle can rescue me as soon as possible.

Timed wait status

Timeout wait is the same as wait state, the only difference is that there is a timeout mechanism, which will not wait for other threads to actively wake up, but will automatically wake up after the specified time.

The following functions trigger the timeout wait state

  • wait(long)
  • join(long)
  • LockSupport.parkNanos(long)
  • LockSupport.parkUtil(long)
  • sleep(long)

The wait(long) and Join (long) functions cause the JVM to put threads into lock wait queues.

The wake up scenario is the same as the wait state, with more automatic wake up action when the timeout expires.

After returning from the Philippines, thread also started illegal activities, and was finally arrested by the police. Fortunately, he was only sentenced to 5 years, so he can get out.

The state of being Blocked

A running thread fails to acquire the synchronization lock or makes an I/O request. The thread is blocked.

The JVM also places the thread in the lock’s synchronization queue if it fails to acquire the lock.

When a synchronization lock is released, the synchronization queue of the lock exits all threads and enters the ready state.

When I/O is complete, the thread returns to the ready state.

After the release of the thread, the life behind has been bumpy, did not go smoothly, we should take warning.

summary

It is believed that readers have mastered the knowledge of thread state by following the idea of A xing. In the future, the interviewer can ask such questions, and you can also mock them, and finally put a simplified thread transformation diagram to end this article.

Personal website

  • Github Pages
  • Gitee Pages