Home  >  Article  >  Java  >  What are the states of java threads

What are the states of java threads

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-12-27 14:53:105096browse

What are the states of java threads

Java threads can be created and destroyed, so threads have a life cycle, and the life cycle of a thread can be described by the six states of the thread.

It can be seen from the source code of the Thread class that there are 6 states of threads:

What are the states of java threads

These six states They are:

1. New: Initial state, the thread is created and start() is not called.

2. Runnable: Running state. Java threads call the two states of ready and running in the operating system "running".

3. Blocked: Blocked, the thread enters the waiting state, and the thread gives up the right to use the CPU for some reason.

Several situations of blocking:

A. Waiting for blocking: The running thread executes wait(), and the JVM will put the current thread into the waiting queue.

B. Synchronous blocking: When the running thread acquires the synchronization lock of the object, if the synchronization lock is occupied by other threads, the JVM will put the current thread into the lock pool.

C. Other blocking: When the running thread executes sleep(), join() or issues an IO request, the JVM will set the current thread to the blocking state. When sleep() is completed, the join() thread terminates , the thread resumes again after IO processing is completed.

4. Waiting: Waiting state.

5. timed_waiting: timeout waiting state, automatically returning after timeout.

6. terminated: Termination status, the current thread has completed execution.

Conversion between the six states of a thread:

When a thread is instantiated, it first enters the initial state, that is, the New state. At this time, the thread starts It does not run immediately, but waits until the operating system schedules it before running, and then calls start() to enter the running state, that is, runnable. The running state includes two states: running and ready. The two states can be converted to each other under the scheduling of the operating system. If the time slice of the running thread is preempted by the CPU, it will become the ready state; the running thread enters the blocked state by calling the synchronized method or synchronized block, that is, blocked. When the thread After acquiring the lock, it enters the running state; if the thread calls sleep(), wait().join(), Locksupported.parkUtil() and other methods during execution, it will enter the waiting state (waiting) or timeout waiting state, that is, timed_waiting, when calling notify(), notifyAll(), Locksupported.unpark() and other methods again, it will re-enter the runtime state. When the thread execution is completed, it will enter the termination state, that is, the terminated state.

Note: Locksupported is a tool class provided by JDK 1.6. In the java.util.concurrent package, the park and unpark methods it provides are more flexible than the wait and notify methods.

View thread status through the command line

First introduce two commands:

1. Jps:JDK 1.5 provides the display of all current Java The process pid command can be used to obtain the corresponding process pid.

2. Jstack: It is a stack tracing tool that comes with the Java virtual machine. It can print out the given process pid information and the Java stack information of the remote debugging service.

Specific steps:

Open the terminal or command line tool, enter the jps command to get the pid;

Enter jstack pid to view the current process information.

What are the states of java threads

PHP Chinese website has a large number of free JAVA introductory tutorials, everyone is welcome to learn!

The above is the detailed content of What are the states of java threads. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn