Hello everyone, I am A xing, long time no see, welcome to Java concurrent programming series extras thread state transitions, the content is easy to understand, please rest at ease to eat.

Thread state

Let’s start with an opening four questions

  • How many Java thread states are there?

  • How does Java thread state transition?

  • When does a Java thread state transition enter a lock wait queue?

  • When does a Java thread state transition enter a 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 jv M to put the thread on the lock wait queue.

Threads in this state are not allocated C P U execution time, they wait to be actively awakened, otherwise they will remain in wait 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 thread to be placed on the lock wait queue.

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.

J V M will also put the thread into the lock 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.

Good historical articles recommended

  • Talk about ReentrantReadWriteLock’s bit operations
  • Easy to understand ReentrantLock, don’t understand you to chop me
  • Word long | 16 picture AbstractQueuedSynchronizer
  • LockSupport for small white look
  • 13 pictures, in-depth understanding of Synchronized
  • From shallow to deep CAS, Xiao Bai can also align with BAT interviewers
  • Small white also can understand the Java memory model
  • Nanny Level teaching, 22 images uncover ThreadLocal
  • Can process, thread and coroutine be confused? A penny for you to know!
  • What is thread safety? This article will give you insight

The last

Code text drawing is not easy, feel the harvest welcome to like, forward, comment, thank you ~

Wechat search the public number “program ape a xing”, make a friend, pull you into the technical communication group

Reply 666, free access to interview materials, quality e-books and resume templates.