Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the state of Java threads?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what the Java thread state is like". In the daily operation, I believe many people have doubts about the Java thread state. The editor consulted all kinds of information and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what the Java thread state is like." Next, please follow the editor to study!

Java thread status

The Java thread state is defined in the Thread.State enumeration, and the state of the current thread can be obtained using the thread#getState method.

The Thread.State status is shown in the following figure:

State.png

You can see that the Java thread has a total of 6 states, which are:

NEW (initial state)

RUNNABLE (running status)

BLOCKED (blocking state)

WATTING (waiting status)

TIMED_WAITING (time limited wait status)

TERMINATED (termination status)

NEW (initial state) and RUNNABLE (running state)

The state of each thread instance just created using new Thread () is in the NEW state, and once thread.start () is called, the thread state will become RUNNABLE.

RUNNABLE (running state) and BLOCKED (blocking state)

A thread in RUNNABLE state will attempt to acquire an implicit exclusive lock before entering a method or code block modified by synchronized. If not, the thread state will become BLOCKED, waiting for the lock to be acquired. Once another thread releases the lock and the thread successfully grabs the lock, the thread state changes from BLOCKED to RUNNABLE state.

RUNNABLE (running status) and WATTING (waiting status)

Threads in the WATTING state will be waiting indefinitely, waiting for other threads to wake up. There are three ways to change a thread from RUNNABLE to WATTING.

Object#wait

After the thread acquires the synchronized implicit lock, the displayed call to the Object#wait () method. In this case, the thread will give up the implicit lock, and once another thread acquires the lock and calls Object.notify () or object.notifyAll (), the thread will wake up and become RUNNABLE.

Thread#join

The join method is a thread synchronization method. Suppose we execute the Thread A.join () method in the main method, and the main thread state becomes WATTING. The main thread will not become RUNNABLE again until thread A finishes executing.

LockSupport#park ()

LockSupport is an important object in JDK concurrency package, and many lock implementations depend on this object. Once LockSupport#park () is called, the thread becomes WATTING. If you need to wake up the thread, you need to call LockSupport#unpark, and then the thread state changes back to RUNNABLE.

RUNNABLE (running status) and TIMED_WAITING (time-limited waiting status)

TIMED_WAITING has the same function as WATTING, except that the former adds the function of time-limited waiting, and once the wait time expires, the thread state automatically changes to RUNNABLE. This state will be triggered in the following situations:

Thread#sleep (long millis)

The thread that owns the synchronized implicit lock calls the Object.wait (long timeout) method

Thread#join (long millis)

LockSupport#parkNanos (Object blocker, long deadline)

LockSupport#parkUntil (long deadline)

RUNNABLE (running status) and TERMINATED (terminating status)

Once a thread finishes execution or an exception occurs during thread execution and does not capture processing normally, the state will automatically become TERMINATED.

The six states of Java threads may seem complicated, but in fact, all of the above BLOCKED,WATTING,TIMED_WAITING will put threads in a dormant state, so we classify all three as dormant. With this classification, the Java thread life cycle can be simplified to the following figure:

Java thread state 2.png generic operating system thread state

After talking about the thread state of the Java system above, let's take a look at the thread state of the general operating system. The operating system thread state can be divided into initial state, runnable state, running state, dormant state and terminated state, as shown in the following figure:

Operating system thread status 1.png

The details of the status in these 5 are as follows:

Initial state, when the thread has just been created and the CPU cannot be allocated.

Runnable state, the thread waits for the system to assign CPU to execute the task.

Running state, the operating system assigns CPU to the thread, and the thread executes the task.

Dormant state, if the running thread calls blocking API, such as blocking to read the file, the thread state will become dormant. In this case, the thread will relinquish the right to use CPU. When the hibernation ends, the thread state will first become runnable.

The end of thread execution or an exception in the execution process will put the thread into a termination state, in which case the thread's mission has ended.

Compare the thread states of the two

Comparing the Java thread with the operating system thread, we can find that the state of the Java thread has no runnable state. In other words, the Java thread RUNNABLE state includes the runnable state and runnable state of the operating system. A Java thread in RUNNABLE state, which may be runnable at the operating system level, is waiting for the system to allocate the right to use CPU.

In addition, the Java thread subdivides the sleep state of the operating system into three types of BLOCKED,WATTING,TIMED_WAITING.

When a thread calls a blocking API, the thread goes to sleep, which refers to the operating system level. From the JVM level, the Java thread state is still in RUNNABLE state. JVM does not care about the actual state of the operating system thread. From the point of view of JVM, waiting for the right to use CPU (the operating system thread state is runnable) is no different from waiting for Imax O (operating system thread state is dormant). They are both waiting for some resource, so they are all classified as RUNNABLE state.

Other Java thread states are similar to operational thread states.

At this point, the study of "what is the state of Java threads" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report