The first article is from my wechat public account: Xiaoxue Java

www.exception.site/java-concur…

Thread lifecycle is one of the most common topics in Java beginner/intermediate interviews. This article is aimed at this problem, through the way of picture and text in detail.

Java thread life cycle

Combined with the above figure, the life cycle of a thread can be roughly divided into the following five states:

  • NEW – NEW
  • RUNNABLE – Wait to be scheduled by CPU
  • RUNNING – RUNNING
  • BLOCKED – block
  • End of the TERMINATED –

I. NEW state

The NEW state represents the state in which a thread is created. Let’s look at an example code:

Thread thread = new Thread(() -> System.out.println("Hello, world !"));
Copy the code

When we create a Thread in our code, it means that the Thread is in the new state, but the Thread has not been created by the operating system. It will only be created after we call the start() method. So to be precise, the NEW state is just the state of the thread object.


What state transitions can a thread in NEW state make?

New State transitions that can occur on a thread

A thread in the NEW state enters the RUNNABLE state after calling the start() method.

RUNNABLE state

When we explicitly call the start() method in our code, the JVM process creates a new thread, which is not immediately scheduled by the CPU to enter the RUNNING state. There is an intermediate state called RUNNABLE state. You can interpret the state as waiting to be scheduled by the CPU:

RUNNABLE Indicates the intermediate state

As shown in the above figure, this means that the thread will go from the NEW state to the RUNNABLE state, waiting for the CPU to schedule it. To put it more simply, this means that the thread is eligible to be executed, but whether it can be executed depends on the CPU’s decision.


What state transitions can a thread in RUNNABLE state make?

Runnable State transition

Threads in RUNNABLE state cannot enter the BLOCKED or TERMINATED state directly.

Many friends here may have a question, why?

Only threads in the RUNNING state, in other words, are qualified to enter the BLOCKED or TERMINATED state if they are assigned CPU execution rights

PS: A thread in the RUNNABLE state can either be converted to the RUNNING state or terminated unexpectedly (e.g. Kill -9 PID).

3. RUNNING state

When CPU scheduling occurs and a RUNNABLE thread is selected in the task queue, the thread enters the RUNNING execution state and starts calling the logic in the run() method.


What state transitions can occur for RUNNING threads?

RUNNING State transition

  • To be translated into TERMINATED state, such as a callstop()Methods;
  • Converted to BLOCKED, as in calledsleep.waitMethods are addedwaitSet;
  • The vm is converted to the BLOCKED state. For example, when I/O blocking is performed, for example, when a database is queried, the VM is BLOCKED.
  • Converted to BLOCKED, such as when a lock is released and added to the blocking queue for that lock;
  • When the time slice of the thread runs out, the CPU schedules the thread again and enters the RUNNABLE state.
  • Thread active callyieldMethod, the CPU resources to enter the RUNNABLE state;

4. BLOCKED state

As we discussed in the previous section, why a thread is BLOCKED, let’s talk directly about what state changes can occur to a thread in the BLOCK state:

BLOCKED state transition

  • To be translated into TERMINATED state, such as a callstop()Method, or the JVM unexpectedly crashes;
  • The state is changed to RUNNABLE and the blocking time is over, such as after reading data from the database;
  • The RUNNABLE state has been set to sleep for the specified time.
  • iswaitIs called by another threadnotify/notifyAllMethod to wake up and enter the RUNNABLE state;
  • The thread acquires the desired lock resource and enters the RUNNABLE state.
  • A thread is interrupted while blocked, as when another thread is calledinterruptMethod to enter the RUNNABLE state;

State TERMINATED

TERMINATED state is the final state of a thread. A thread in TERMINATED state does not switch to any of these states. Once it is in TERMINATED state, it is TERMINATED and there is no turning back.

Threads enter TERMINATED state in the following cases:

  • The normal running of the thread ends, and the life cycle ends.
  • An unexpected error occurred during thread execution.
  • The JVM terminates abnormally and all thread lifetimes are terminated.

When is the run() method called?

Through the graphic, we understand the five states of the thread life cycle, next, let’s look at the start method source code, in fact, the internal source code is very simple, as shown below:

Start method internal source code

  • ① : First, it will determine whether the thread state is NEW state, internal corresponding state identifier is 0, that is to say, if not equal to 0, directly throw thread state exception;
  • 2.: Thread is added to after startupThreadGroup;
  • 3.: start0Is the core method, is the running state of the NEW (internal state identifier 0) thread;
  • (4):start0Is anativeMethod, which is JNI method;

If you look at this, you might be wondering, when is the run method you overwrote called? Source code also did not see call ah!!

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

This is an excerpt from the JDK documentation.

The run method is called when the JNI method start0() is called to execute the logic we wrote.

What is the difference between the start method and the run method of a thread?

I believe that after reading the above source analysis, friends must be able to source the Angle of hatred back!

Seven,

In this paper, Ha explained the five states of the thread by means of pictures and texts, as well as the states that can be converted. Finally, we take a quick look at the start() internal source code to see when the run() method is executed. Finally, I hope those of you who have read this article have learned something. See you next time!

Eight, Ref

  • Java High Concurrency Programming in Detail

Welcome to follow the wechat public number: Xiaoxue Java

Hargreaves to learn Java

Gifts | interview & learning welfare resources

Recently found a good PDF resource on the Internet “Java core interview knowledge. PDF” to share with you, not only interview, learning, you are worth having!!

How to obtain: Follow wechat official account: XiaoxuxueJava, reply “resources” in the background, you can get the resource link for free without any routine, the following is the catalog and some screenshots:

Screenshot of Welfare Resources

Screenshot of Welfare Resources

Screenshot of Welfare Resources

Screenshot of Welfare Resources

Screenshot of Welfare Resources

Screenshot of Welfare Resources

Screenshot of Welfare Resources

Important things to say twice, access: pay attention to wechat public number: ha learning Java, background reply “resources”, both free without routine access to resources links!!